HTTP connection base class, responsible for the underlying HTTP communication

Source: Internet
Author: User

<summary>//HTTP connection base class, responsible for the underlying HTTP communication///</summary> public class Httpservice {public St        atic bool CheckValidationResult (object sender, X509Certificate certificate, X509chain chain, sslpolicyerrors errors)        {//Direct confirmation, otherwise it will not open return true; public static string Post (string xml, string url, int timeout) {System.GC.Collect ();//garbage collection, recycling no            HTTP connection with normal shutdown string result = "";//return result HttpWebRequest request = null;            HttpWebResponse response = null;            Stream reqstream = null;                try {//Set maximum number of connections Servicepointmanager.defaultconnectionlimit = 200; Set the HTTPS authentication method if (URL. StartsWith ("https", StringComparison.OrdinalIgnoreCase)) {Servicepointmanager.servercer Tificatevalidationcallback = new Remotecertificatevalidationcallback (CheckvalidaTionresult);                 }/*************************************************************** * HttpWebRequest related properties are set below * ************************************************************/request = (HttpWebRequest) W                Ebrequest.create (URL); Request.                Method = "POST"; Request.                Timeout = timeout * 1000;                          Set Proxy server//webproxy proxy = new WebProxy (); Defines a gateway object//proxy.              Address = new Uri (Wxpayconfig.proxy_url); Gateway server port: Port//request.                Proxy = proxy; Set the data type and length of the POST request.                ContentType = "Application/json";                byte[] data = System.Text.Encoding.UTF8.GetBytes (XML); Request. ContentLength = data.                Length; Write data to the server Reqstream = Request.                GetRequestStream (); Reqstream.write (data, 0, data.                Length); Reqstream.cloSE (); Gets the server return response = (HttpWebResponse) request.                GetResponse (); Gets the service-side return data StreamReader sr = new StreamReader (response.                GetResponseStream (), Encoding.UTF8); result = Sr. ReadToEnd ().                Trim (); Sr.            Close (); } catch (System.Threading.ThreadAbortException e) {writeerrorlog.writelogweb ("Httpse                Rvice, "+" Thread-caught threadabortexception-resetting. ");                Writeerrorlog.writelogweb ("Exception message: {0}" + e.message);            System.Threading.Thread.ResetAbort ();                } catch (WebException e) {writeerrorlog.writelogweb ("httpservice," + e.tostring ()); if (E.status = = webexceptionstatus.protocolerror) {Writeerrorlog.writelog Web ("Httpservice," + "StatusCode:" + ((HttpWebResponse) e.response).                    StatusCode); Writeerrorlog.writelogwEB ("httpservice," + "Statusdescription:" + ((HttpWebResponse) e.response).                Statusdescription);            } throw E;                } catch (Exception e) {writeerrorlog.writelogweb ("httpservice," + e.tostring ());            Throw e;                    } finally {//closes the connection and flows if (response! = NULL) { Response.                Close (); } if (Request! = NULL) {request.                Abort ();        }} return result; }///<summary>//To process HTTP GET requests, return data///</summary>//<param name= "url" > please The URL address of the request </param>///<returns>http Get successfully returned data, failed to throw WebException exception </returns> public static St            Ring Get (string url) {System.GC.Collect ();            string result = ""; Writeerrorlog.writelogweb ("HttpservIce Get Request Address: "+ url");            HttpWebRequest request = null;            HttpWebResponse response = null; Request URL to get data try {//Set maximum number of connections Servicepointmanager.defaultconnectionlimi                t = 200; Set the HTTPS authentication method if (URL. StartsWith ("https", StringComparison.OrdinalIgnoreCase)) {Servicepointmanager.servercer                Tificatevalidationcallback = new Remotecertificatevalidationcallback (CheckValidationResult); }/*************************************************************** * Set HTTPW below Ebrequest Related properties * ************************************************************/request = (Ht                tpwebrequest) webrequest.create (URL); Request.                Method = "GET";                Set proxy//webproxy proxy = new WebProxy (); Proxy. Address = new Uri (wxpayconfig.proxy_url); Request.                Proxy = proxy; Gets the server return response = (HttpWebResponse) request.                GetResponse (); Gets the HTTP return data StreamReader sr = new StreamReader (response.                GetResponseStream (), Encoding.UTF8); result = Sr. ReadToEnd ().                Trim (); Sr.            Close (); } catch (System.Threading.ThreadAbortException e) {writeerrorlog.writelogweb ("Httpse                Rvice "+", Thread-caught threadabortexception-resetting. ");                Writeerrorlog.writelogweb ("Exception message: {0}" + e.message);            System.Threading.Thread.ResetAbort ();                } catch (WebException e) {writeerrorlog.writelogweb ("Httpservice" + e.tostring ()); if (E.status = = webexceptionstatus.protocolerror) {Writeerrorlog.writelog Web ("Httpservice" + ", StatusCode:" + ((HttpWebResponse) e.response).        StatusCode);            Writeerrorlog.writelogweb ("Httpservice" + ", Statusdescription:" + ((HttpWebResponse) e.response).                Statusdescription);            } throw E;                } catch (Exception e) {writeerrorlog.writelogweb ("httpservice," + e.tostring ());            Throw e;                    } finally {//closes the connection and flows if (response! = NULL) { Response.                Close (); } if (Request! = NULL) {request.                Abort ();        }} return result; }    }

  

HTTP connection base class, responsible for the underlying HTTP communication

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.