Some processing of C # HttpWebRequest HTTPS

Source: Internet
Author: User

First look at the request method

1    Public stringGet_request (2             stringstrURL,3Cookiecontainer _cookie =NULL,4             stringStrhost ="",5             stringStrrefer ="",6             stringStrorigin ="",7             BOOLBlnhttps =false,8dictionary<string,string> lstheads =NULL,9             BOOLBlnkeepalive=false,Ten             stringStrencoding ="Utf-8", One             stringStrcontenttype ="", A             stringstrcertfile="", -             stringStraccept ="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", -             stringStruseragent ="mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36", the             BOOLBlnallowautoredirect =true, -             intInttimeout = +* -) -         { - HttpWebRequest request; + HttpWebResponse response; -Request =(HttpWebRequest) webrequest.create (strURL); +             if(BLNHTTPS) A             { atServicepointmanager.servercertificatevalidationcallback =NewRemotecertificatevalidationcallback (CheckValidationResult);  -Request. ProtocolVersion =Httpversion.version10; -  -Servicepointmanager.securityprotocol =securityprotocoltype.tls12; -  -             }            inRequest. KeepAlive =blnkeepalive; -Request. Accept =straccept; toRequest. Timeout =inttimeout; +Request. Method ="GET"; -Request. Credentials =CredentialCache.DefaultCredentials; theRequest. UserAgent =struseragent; *Request. AllowAutoRedirect =Blnallowautoredirect; $Request. Proxy =NULL;Panax Notoginseng             if(!string. IsNullOrEmpty (Strcontenttype)) -             { theRequest. ContentType =Strcontenttype; +             } A             if(_cookie! =NULL) the             { +Request. Cookiecontainer =_cookie; -             } $             if(!string. IsNullOrEmpty (strhost)) $             { -Request. Host =Strhost; -             } the             if(!string. IsNullOrEmpty (Strrefer)) -             {WuyiRequest. Referer =Strrefer; the             } -             if(!string. IsNullOrEmpty (strorigin)) Wu             { -Request. Headers.add ("Origin", strorigin); About             } $             if(Lstheads! =NULL&& Lstheads.count >0) -             { -                 foreach(varIteminchlstheads) -                 { A request. Headers.add (item. Key, item. Value); +                 } the             } -Response =(HttpWebResponse) request. GetResponse (); $             varSR =NewStreamReader (response. GetResponseStream (), encoding.getencoding (strencoding)); the             stringStrresult =Sr. ReadToEnd (); the Sr. Close (); the request. Abort (); the Response. Close (); -             returnstrresult; in  the         } the         Private Static BOOLCheckValidationResult (Objectsender, X509Certificate certificate, X509chain chain, sslpolicyerrors errors) About         { the             return true;//Always accept the}
The code within the IF (BLNHTTPS) is the processing done for HTTPS

It is important to note that

1, when the use of HTTPS requests need to determine which encryption protocol is, this can be found through Firefox, such as

2. Only Framework4.5 and above support 1.1 and 1.2 protocol

If there's still something you don't understand, leave a message.

Some processing of C # HttpWebRequest HTTPS

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.