The following content, reproduced in the online one of the great God, to save their own spare
//body is the parameter to be passed, the format "roleid=1&uid=2"//Post Cotenttype fill in://"application/x-www-form-urlencoded"//soap fills in: "Text/xml; Charset=utf-8 " Public Static stringPosthttp (stringUrlstringBodystringContentType) {HttpWebRequest HttpWebRequest=(HttpWebRequest) webrequest.create (URL); Httpwebrequest.contenttype=ContentType; Httpwebrequest.method="POST"; Httpwebrequest.timeout=20000; byte[] Btbodys =Encoding.UTF8.GetBytes (body); Httpwebrequest.contentlength=btbodys.length; Httpwebrequest.getrequeststream (). Write (Btbodys,0, btbodys.length); HttpWebResponse HttpWebResponse=(HttpWebResponse) httpwebrequest.getresponse (); StreamReader StreamReader=NewStreamReader (Httpwebresponse.getresponsestream ()); stringResponsecontent =Streamreader.readtoend (); Httpwebresponse.close (); Streamreader.close (); Httpwebrequest.abort (); Httpwebresponse.close (); returnresponsecontent; }Post Method (HttpWebRequest)
/// <summary> ///Basic authentication is required by WebClient post data to remote address;///The caller handles the exception itself/// </summary> /// <param name= "uri" ></param> /// <param name= "Paramstr" >name= Zhang San &age=20</param> /// <param name= "encoding" >Please confirm the encoding of the target page first</param> /// <param name= "username" ></param> /// <param name= "password" ></param> /// <returns></returns> Public Static stringRequest_webclient (stringUristringParamstr, Encoding Encoding,stringUsernamestringpassword) { if(Encoding = =NULL) Encoding=Encoding.UTF8; stringresult =string. Empty; WebClient WC=NewWebClient (); //the header that must be added in the post modeWc. Headers.add ("Content-type","application/x-www-form-urlencoded"); byte[] PostData =encoding. GetBytes (PARAMSTR); if(!string. IsNullOrEmpty (username) &&!string. IsNullOrEmpty (password)) {WC. Credentials=Getcredentialcache (URI, username, password); Wc. Headers.add ("Authorization", getauthorization (username, password)); } byte[] responsedata = WC. Uploaddata (URI,"POST", PostData);//get a stream of returned characters returnEncoding. GetString (ResponseData);//decoding}Post Method (WebClient)
Public Static stringGethttp (stringURL, HttpContext HttpContext) { stringQueryString ="?"; foreach(stringKeyinchhttpContext.Request.QueryString.AllKeys) {QueryString+ = key +"="+ Httpcontext.request.querystring[key] +"&"; } queryString= Querystring.substring (0, Querystring.length-1); HttpWebRequest HttpWebRequest= (HttpWebRequest) webrequest.create (URL +queryString); Httpwebrequest.contenttype="Application/json"; Httpwebrequest.method="GET"; Httpwebrequest.timeout=20000; //byte[] Btbodys = Encoding.UTF8.GetBytes (body); //httpwebrequest.contentlength = btbodys.length; //Httpwebrequest.getrequeststream (). Write (Btbodys, 0, btbodys.length);HttpWebResponse HttpWebResponse=(HttpWebResponse) httpwebrequest.getresponse (); StreamReader StreamReader=NewStreamReader (Httpwebresponse.getresponsestream ()); stringResponsecontent =Streamreader.readtoend (); Httpwebresponse.close (); Streamreader.close (); returnresponsecontent; }Get Method (HttpWebRequest)
/// <summary> ///the Webrequest/webresponse class accesses the remote address and returns the result, requiring basic authentication;///The caller handles the exception itself/// </summary> /// <param name= "uri" ></param> /// <param name= "Timeout" >the access time-out, in milliseconds, or 0 if no time-out is set</param> /// <param name= "encoding" >If you do not know the specific encoding, pass in null</param> /// <param name= "username" ></param> /// <param name= "password" ></param> /// <returns></returns> Public Static stringRequest_webrequest (stringUriintTimeout, Encoding Encoding,stringUsernamestringpassword) { stringresult =string. Empty; WebRequest Request= WebRequest.Create (NewUri (URI)); if(!string. IsNullOrEmpty (username) &&!string. IsNullOrEmpty (password)) {request. Credentials=Getcredentialcache (URI, username, password); Request. Headers.add ("Authorization", getauthorization (username, password)); } if(Timeout >0) Request. Timeout=timeout; WebResponse response=request. GetResponse (); Stream Stream=Response. GetResponseStream (); StreamReader SR= Encoding = =NULL?NewStreamReader (Stream):NewStreamReader (stream, encoding); Result=Sr. ReadToEnd (); Sr. Close (); Stream. Close (); returnresult; } #region# Generate Http Basic Access Credentials #Private StaticCredentialCache Getcredentialcache (stringUristringUsernamestringpassword) { stringauthorization =string. Format ("{0}:{1}", username, password); CredentialCache Credcache=NewCredentialCache (); Credcache.add (NewUri (URI),"Basic",Newnetworkcredential (username, password)); returnCredcache; } Private Static stringGetauthorization (stringUsernamestringpassword) { stringauthorization =string. Format ("{0}:{1}", username, password); return "Basic"+ convert.tobase64string (Newasciiencoding (). GetBytes (authorization)); } #endregionWebrequest/webresponse of basic validation
C # Post/get method for implementing HTTP via Webclient/httpwebrequest (reprint)