Record HttpWebRequest Auxiliary class

Source: Internet
Author: User

Recently because of the working relationship, need to go through the HttpWebRequest to request the API, all write a simple helper class, make a backup.

 Public Sealed classHttprequesthelper {Private Static stringUser_agent ="mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/37.0.2062.124 safari/537.36"; Private Static stringACCEPT ="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Private Static stringContent_Type ="application/x-www-form-urlencoded; Encoding=utf-8";
/// <summary> ///Single Case/// </summary> Private Static volatileHttprequesthelper instance; Private Static ObjectSyncRoot =New Object(); PrivateHttprequesthelper () {} Public Statichttprequesthelper Instance {Get { if(Instance = =NULL) { Lock(syncRoot) {if(Instance = =NULL) {instance=NewHttprequesthelper (); } } } returninstance; } } /// <summary> ///GET Request/// </summary> /// <param name= "queryString" >URL Parameters</param> /// <returns>JSON format Results</returns> Public stringGet (stringURL) { stringRET =string. Empty; URL= URL. Replace ("Access_token", WxTokenHelper.Instance.Token); varRequest = Createrequest ("GET", URL); varResponse =request. GetResponse (); Stream Responsestream=Response. GetResponseStream (); StreamReader Readstream=NewStreamReader (Responsestream, Encoding.UTF8); RET=Readstream.readtoend (); Readstream.close (); returnret; } Public stringGetToken (stringURL) { stringRET =string. Empty; varRequest = Createrequest ("GET", URL); varResponse =request. GetResponse (); Stream Responsestream=Response. GetResponseStream (); StreamReader Readstream=NewStreamReader (Responsestream, Encoding.UTF8); RET=Readstream.readtoend (); Readstream.close (); returnret; } /// <summary> ///POST Request/// </summary> /// <param name= "URL" >the requested URL</param> /// <param name= "Jsondata" >JSON format Parameters</param> /// <returns>JSON format Results</returns> Public stringPost (stringUrlstringjsondata) { stringRET =string. Empty; URL= URL. Replace ("Access_token", WxTokenHelper.Instance.Token); varRequest = Createrequest ("POST", URL); if(!string. Isnullorwhitespace (Jsondata)) {byte[] data =Encoding.UTF8.GetBytes (Jsondata); Request. ContentLength=data. Length; using(varRequeststream =request. GetRequestStream ()) {requeststream.write (data,0, data. Length); }; varResponse = Request. GetResponse () asHttpWebResponse; using(varResponsestream =Response. GetResponseStream ()) {using(varReadstream =NewStreamReader (Responsestream, Encoding.UTF8)) {ret=Readstream.readtoend (); } } } returnret; } PrivateHttpWebRequest Createrequest (stringMethodstringURL) {HttpWebRequest Request=(HttpWebRequest) webrequest.create (URL); Request. Accept=ACCEPT; Request. UserAgent=user_agent; Request. Method=method; Request. ContentType=Content_Type; returnrequest; } }

Record HttpWebRequest Auxiliary class

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.