About HttpWebRequest and httpwebrequest

Source: Internet
Author: User

About HttpWebRequest and httpwebrequest

Using System; using System. collections. generic; using System. net; // use the class library using System, such as httpwebresponse. IO; // serialize using System. text; // text namespace MyTestHttpWebRequest {public class Test {// <summary> // message submission /// </summary> /// <param name = "str"> required submitted content </param> /// <param name = "url"> address </param> /// <returns> </returns> public string TestHttpWebRequest (string str, string url) {HttpWebRequest httpwebrequest = WebRequest. create (url) as HttpWebRequest; // define // -- enter the header -- // httpwebrequest. contentType = "application/x-www-form-urlencoded"; // submit the httpwebrequest in the from format. accept = "*/*"; httpwebrequest. userAgent = "Mozilla-Firefox-Spider (Kolee)"; // Service proxy // -- submit Method -- // httpwebrequest. method = "POST"; // -- Obtain information -- // Stream stream = httpwebrequest. getResponse (). getResponseStream (); // get stream // -------------------- write ------------------ // StreamWriter streamwriter = new StreamWriter (stream); // put it into streamwriter. write (str); // Write stream streamwriter. flush (); // push stream // -------------------- read ------------------- // StreamReader streamreader = new StreamReader (stream); // put the string resout = streamreader into the read stream. readToEnd (); // read the stream result streamwriter. close (); streamreader. close (); return resout; // return the returned value read from the webpage} // here is the part of the submitted page to read public partial class _ Default: System. web. UI. page // inherit the page {protected void Page_Load (object sender, EventArgs e) {if (Request. httpMethod = "POST") // select the submission method {Stream stream = Request. inputStream; // get StreamReader str = new StreamReader (stream, System. text. encoding. UTF8); // put it into the read stream Response. write (str. readToEnd (). toString () + "

 

// Note that if the url has been submitted by POST. Must be accurate to the page

---------------------------- If you cannot use it, try the byte method -----------------------

Using System; using System. collections. generic; using System. linq; using System. text; using System. net; using System. IO; namespace RestApiSdk {public class HttpPost {public string SendAll (string Url, string jsonString) {# region sends http post data HttpWebRequest httpWebRequest = WebRequest. create (Url) as HttpWebRequest; httpWebRequest. method = "POST"; httpWebRequest. contentType = "application/json"; byte [] data = System. text. encoding. UTF8.GetBytes (jsonString); using (Stream stream = httpWebRequest. getRequestStream () {stream. write (data, 0, data. length);} Stream responseStream = httpWebRequest. getResponse (). getResponseStream (); string result = string. empty; using (StreamReader responseReader = new StreamReader (responseStream, Encoding. UTF8) {result = responseReader. readToEnd ();} return result; # endregion }}}

 

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.