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