Body is the parameter to be passed, the format "roleid=1&uid=2"//post cotenttype Fill in://"application/x-www-form-urlencoded"//soap fill in: "text/ xml Charset=utf-8 "public static string posthttp (string URL, string body, string contentType) {HttpWebRequest HT Tpwebrequest = (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 = new StreamReader (Httpwebresponse.getresponsestream ()); String responsecontent = Streamreader.readtoend (); Httpwebresponse.close (); Streamreader.close (); Httpwebrequest.abort (); Httpwebresponse.close (); return responsecontent; }postMethod (HttpWebRequest)
<summary>////via WebClient class post data to remote address, Basic authentication is required;///The caller handles the exception itself///</summary> <param name= "uri" ></param>///<param name= "Paramstr" >name= Zhang San &age=20</param> <param name= "Encoding" > Please first confirm the encoding of the target page </param>//<param name= "username" ></param> <param name= "Password" ></param>///<returns></returns> public static string R Equest_webclient (String uri, String paramstr, Encoding Encoding, string Username, string password) {if (encoding = = null) encoding = ENCODING.UTF8; string result = String. Empty; WebClient WC = new WebClient (); The header WC must be added to the post method. 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); Gets the return character stream return encoding. GetString (responsedata);//decoding}post method (WebClient)
public static string gethttp (string URL, HttpContext HttpContext) {string queryString = "?"; foreach (string key in HttpContext.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 = new StreamReader (Httpwebresponse.getresponsestream ()); String responsecontent = Streamreader.readtoend (); Httpwebresponse.close (); Streamreader.close (); return responsecontent; }get Method (HttpWebRequest)
<summary>////The Webrequest/webresponse class accesses the remote address and returns the result, which requires basic authentication;///The caller handles the exception by itself///</summ ary>//<param name= "uri" ></param>///<param Name= "Timeout" > Access time-out, in milliseconds; If you do not set a time-out, pass in 0 </param>//<param name= "encoding" > If you do not know the specific encoding, incoming null</param>//<param name= "username "></param>//<param name=" password "></param>//<returns></returns> public static string Request_webrequest (string uri, int timeout, Encoding Encoding, string Username, string password) {string result = string. Empty; WebRequest request = WebRequest.Create (new Uri (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? New StreamReader (Stream): New StreamReader (stream, encoding); result = Sr. ReadToEnd (); Sr. Close (); Stream. Close (); return result; } #region # generates an Http Basic access voucher # private static CredentialCache Getcredentialcache (string uri, String Userna Me, String password) {string authorization = string. Format ("{0}:{1}", username, password); CredentialCache Credcache = new CredentialCache (); Credcache.add (new Uri (URI), "Basic", new NetworkCredential (username, password)); return credcache; } private static string Getauthorization (string username, string password) {string authorization = String. Format ("{0}:{1}", username, password); Return "Basic" + convert.tobase64string (new ASCIIEncoding (). GetBytes (authorization)); } #endregionbasic验证的WebRequest/webresponse
C # Post/get method for implementing HTTP through Webclient/httpwebrequest