C # Use WebClient/HttpWebRequest to implement the http POST/GET method,
/// <Summary> /// the body is the parameter to be passed, enter "application/x-www-form-urlencoded" in the format of "roleId = 1 & uid = 2" // enter "application/x-www-form-urlencoded" // enter "text/xml; charset = UTF-8 "// </summary> public static string PostHttp (string url, string body, string contentType) {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 = new StreamReader (httpWebResponse. getResponseStream (); string responseContent = streamReader. readToEnd (); httpWebResponse. close (); streamReader. close (); httpWebRequest. abort (); httpWebResponse. close (); return responseContent ;}
1 /// <summary> 2 // use the WebClient Post class to send data to a remote address, which requires Basic authentication; 3 // The caller handles the exception by himself. 4 // </summary> 5 // <param name = "uri"> </param> 6 // <param name = "paramStr"> name = zhangsan & age = 20 </param> 7 // <param name = "encoding"> confirm the encoding method of the target webpage </param> 8 /// <param name = "username"> </param> 9 /// <param name = "password"> </param> 10 /// <returns> </returns> 11 public static string Request_WebClient (string uri, string paramStr, Enc Oding encoding, string username, string password) 12 {13 if (encoding = null) 14 encoding = Encoding. UTF8; 15 16 string result = string. empty; 17 18 WebClient wc = new WebClient (); 19 20 // The Header21 wc that must be added to the POST method. headers. add ("Content-Type", "application/x-www-form-urlencoded"); 22 23 byte [] postData = encoding. getBytes (paramStr); 24 25 if (! String. IsNullOrEmpty (username )&&! String. isNullOrEmpty (password) 26 {27 wc. credentials = GetCredentialCache (uri, username, password); 28 wc. headers. add ("Authorization", GetAuthorization (username, password); 29} 30 31 byte [] responseData = wc. uploadData (uri, "POST", postData); // get the response stream 32 return encoding. getString (responseData); // decodes 33}
1 public static string GetHttp (string url, HttpContext httpContext) 2 {3 string queryString = "? "; 4 5 foreach (string key in httpContext. request. queryString. allKeys) 6 {7 queryString + = key + "=" + httpContext. request. queryString [key] + "&"; 8} 9 10 queryString = queryString. substring (0, queryString. length-1); 11 12 HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest. create (url + queryString); 13 14 httpWebRequest. contentType = "application/json"; 15 httpWebRequest. method = "GET"; 16 httpWebRequest. timeout = 20000; 17 18 // byte [] btBodys = Encoding. UTF8.GetBytes (body); 19 // httpWebRequest. contentLength = btBodys. length; 20 // httpWebRequest. getRequestStream (). write (btBodys, 0, btBodys. length); 21 22 HttpWebResponse httpWebResponse = (HttpWebResponse) httpWebRequest. getResponse (); 23 StreamReader streamReader = new StreamReader (httpWebResponse. getResponseStream (); 24 string responseContent = streamReader. readToEnd (); 25 26 httpWebResponse. close (); 27 streamReader. close (); 28 29 return responseContent; 30}HttpWebRequest (GET)
1 /// <summary> 2 /// use the WebRequest/WebResponse class to access the remote address and return the result. Basic authentication is required; 3 // The caller handles the exception by himself. 4 // </summary> 5 // <param name = "uri"> </param> 6 // <param name = "timeout"> access timeout, the Unit is milliseconds. If no timeout value is set, enter 0 </param> 7 // <param name = "encoding"> if you do not know the specific encoding, input null </param> 8 // <param name = "username"> </param> 9 // <param name = "password"> </param> 10/ // <returns> </returns> 11 public static string Request_WebRequest (st Ring uri, int timeout, Encoding encoding, string username, string password) 12 {13 string result = string. empty; 14 15 WebRequest request = WebRequest. create (new Uri (uri); 16 17 if (! String. IsNullOrEmpty (username )&&! String. isNullOrEmpty (password) 18 {19 request. credentials = GetCredentialCache (uri, username, password); 20 request. headers. add ("Authorization", GetAuthorization (username, password); 21} 22 23 if (timeout> 0) 24 request. timeout = timeout; 25 26 WebResponse response = request. getResponse (); 27 Stream stream = response. getResponseStream (); 28 StreamReader sr = encoding = null? New StreamReader (stream): new StreamReader (stream, encoding); 29 30 result = sr. readToEnd (); 31 32 sr. close (); 33 stream. close (); 34 35 return result; 36} 37 38 # region # generate Http Basic Access Credential #39 40 private static CredentialCache GetCredentialCache (string uri, string username, string password) 41 {42 string authorization = string. format ("{0 }:{ 1}", username, password); 43 44 CredentialCache credCache = new CredentialCache (); 45 credCache. add (new Uri (uri), "Basic", new NetworkCredential (username, password); 46 47 return credCache; 48} 49 50 private static string GetAuthorization (string username, string password) 51 {52 string authorization = string. format ("{0 }:{ 1}", username, password); 53 54 return "Basic" + Convert. toBase64String (new ASCIIEncoding (). getBytes (authorization); 55} 56 57 # endregion