C # Background Access interface

Source: Internet
Author: User

Directly on the code

Background code

//interface address
StringURL ="http://spherefg.topsmoon.com:6666/restapi/Comment/SubmitCommentFromPC?commentPeople=1067&comment=%E4% B8%ad%e8%af%84&content=%e6%b5%8b%e8%af%95&taskid=1160&userid=1";//get request HttpWebResponse W= Httphelper.creategethttpresponse (URL,30000,NULL,NULL); StreamReader SR=NewStreamReader (W.getresponsestream ()); string/*This is a JSON string*/Jsonstr = Sr. ReadToEnd ();

Tool class:

Using system;using system.collections.generic;using system.io;using system.net;using System.Net.Security;using System.security.cryptography.x509certificates;using System.text;public class httphelper{//<summary>//Chong HTTP request to build get mode///</summary>//public static HttpWebResponse creategethttpresponse (string url, int t        Imeout, String useragent, Cookiecollection cookie) {HttpWebRequest request = null; if (URL. StartsWith ("https", StringComparison.OrdinalIgnoreCase)) {//Validation of server-side certificates (certificates issued by non-third-party authorities, such as self-generated, not verified, here Returns true) Servicepointmanager.servercertificatevalidationcallback = new Remotecertificatevalidationcallback (Check            Validationresult);            Request = WebRequest.Create (URL) as HttpWebRequest; Request.    ProtocolVersion = Httpversion.version10;        HTTP version, default is 1.1, here is set to 1.0} else {request = WebRequest.Create (URL) as HttpWebRequest; } request. MethOD = "GET"; Set proxy useragent and timeout//request.        useragent = useragent; Request.        Timeout = timeout; if (cookie = null) {request.            Cookiecontainer = new Cookiecontainer (); Request.        Cookiecontainer.add (cookies); } return request.    GetResponse () as HttpWebResponse; }///<summary>//Create an HTTP request for POST method///</summary> public static HttpWebResponse Createposth Ttpresponse (string URL, idictionary<string, string> parameters, int timeout, string useragent, Cookiecollection        Cookies) {HttpWebRequest request = null; If the HTTPS request is sent if (URL. StartsWith ("https", StringComparison.OrdinalIgnoreCase)) {//servicepointmanager.servercertificatevalida            Tioncallback = new Remotecertificatevalidationcallback (CheckValidationResult);            Request = WebRequest.Create (URL) as HttpWebRequest; Request.        ProtocolVersion = Httpversion.version10;       } else {request = WebRequest.Create (URL) as HttpWebRequest; } request.        Method = "POST"; Request.        ContentType = "application/x-www-form-urlencoded"; Set proxy useragent and timeout//request.        useragent = useragent; Request.         Timeout = timeout; if (cookie = null) {request.            Cookiecontainer = new Cookiecontainer (); Request.        Cookiecontainer.add (cookies); }//Send post data if (! ( Parameters = = NULL | | Parameters.            Count = = 0) {StringBuilder buffer = new StringBuilder ();            int i = 0; foreach (string key in Parameters. Keys) {if (i > 0) {buffer.                AppendFormat ("&{0}={1}", Key, Parameters[key]); } else {buffer.                    AppendFormat ("{0}={1}", Key, Parameters[key]);                i++; }} byte[] Data= Encoding.ASCII.GetBytes (buffer.            ToString ()); using (Stream stream = Request. GetRequestStream ()) {stream. Write (data, 0, data.            Length); }} string[] values = Request.        Headers.getvalues ("Content-type"); Return request.    GetResponse () as HttpWebResponse; }///<summary>///Get the requested data///</summary> public static string getresponsestring (HttpWebResponse WebResponse) {using (Stream s = WebResponse.            GetResponseStream ()) {StreamReader reader = new StreamReader (s, Encoding.UTF8); Return reader.        ReadToEnd (); }}///<summary>//Authentication certificate///</summary> private static bool CheckValidationResult (object send            Er, x509certificate certificate, X509chain chain, sslpolicyerrors errors) {if (Errors = = Sslpolicyerrors.none)        return true;    return false; }}

C # Background Access interface

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.