C # about the Send WebRequest method and data processing
Source: Internet
Author: User
A long time not to update the blog, this time about the C # in the Send WebRequest, well-known,. NET network programming is very common in testing, specifically, for example, if we test an API, through the way of Send WebRequest API-level functional testing, and then use selenium to complete the front-end verification, this mode should be said to be more convenient and universal, Specifically, for example, the most common HTTP request, when we need to transfer data, we need to
1) Address: Addr As the WebRequest parameter instantiation, and then set the corresponding properties, such as method (get or post, etc.)
2) Data: Data stream is written as HttpRequest:
Parameter Processing Section
Format the data
string output = data. Aggregate (String.Empty, (current, KeyValuePair) and current + ("&" + Keyvaluepair.key + "=" + Httputility.urlencode ( (Keyvaluepair.value)));
var encoding = new UTF8Encoding ();
Set the postdata variable to process output and finally get byte[] D as final data written to WebRequest
var postdata = Encoding.ASCII.GetBytes (output);
Byte[] D = Encoding. GetBytes (output. Substring (1, output. LENGTH-1));
var address = TargetUrl;
Data processing in the Get section
if (method. Toupperinvariant () = = "GET")
Address=address+output;
Data processing in the post section
WebRequest request = WebRequest.Create (address) as HttpWebRequest;
ADD Post Process
if (request = = null)
throw new Exception ("WebRequest object is null.");
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.