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:

Xmlresponse = null;
Responsecode = httpstatuscode.unused;
Serverresponse = String.Empty;

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.");

Request. Method = "POST";
Request. ContentType = "application/x-www-form-urlencoded";
Request. ContentLength = D.length;

Serverresponse = String.Empty;

Data Read and Processing Section
using (Stream DataStream = Request. GetRequestStream ())
{
Datastream.write (d, 0, d.length);
Datastream.close ();

using (var response = (HttpWebResponse) request. GetResponse ())
{
Responsecode = Response. StatusCode;
using (Stream MyStream = response. GetResponseStream ())
{
if (mystream! = null)
{
var readstream = new StreamReader (MyStream, Encoding.UTF8);
Serverresponse = Readstream.readtoend ();
}
}
Response. Close ();
}
}


if (Responsecode = = Httpstatuscode.ok)
{
if (LOADXML)
LOADXML ();
return true;
}

return false;
  • Related Article

    Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.