Recently the company project needs to butt HTTPS interface, the docking code is organized as follows:
1 Public voidGet ()2 {3HttpWebRequest request =NULL;4Request = WebRequest.Create ("Https://10.15.67.27:8073/api/tvwall") asHttpWebRequest;5Servicepointmanager.servercertificatevalidationcallback =NewRemotecertificatevalidationcallback (checkvalidationresult);6Request. ProtocolVersion =httpversion.version11;7 //the protocol type is set here. 8Servicepointmanager.securityprotocol = (securityprotocoltype)3072; ;//securityprotocoltype.tls1.2;9Request. KeepAlive =false;TenServicepointmanager.checkcertificaterevocationlist =true; OneServicepointmanager.defaultconnectionlimit = -; AServicepointmanager.expect100continue =false; - -Request. Method ="GET";//send data using get mode theRequest. ContentType ="Application/json"; -Request. Accept ="Application/json"; -Request. Referer =NULL; -Request. AllowAutoRedirect =true; + -HttpWebResponse response =(HttpWebResponse) request. GetResponse (); +Stream stream =Response. GetResponseStream (); A stringresult =string. Empty; at using(StreamReader sr =NewStreamReader (stream)) - { -result =Sr. ReadToEnd (); - } - Console.Write (result); -}
1 Public voidPost ()2 {3Stream Formdatastream =NewSystem.IO.MemoryStream ();4HttpWebRequest request =NULL;5 stringFormdataboundary = String.Format ("----------{0:n}", Guid.NewGuid ());6Request = WebRequest.Create ("https://10.15.67.27:8073/api/tvwall/25/scene/new") asHttpWebRequest;7Servicepointmanager.servercertificatevalidationcallback =NewRemotecertificatevalidationcallback (checkvalidationresult);8Request. ProtocolVersion =httpversion.version11;9 //the protocol type is set here. TenServicepointmanager.securityprotocol = (securityprotocoltype)3072;//securityprotocoltype.tls1.2; OneRequest. KeepAlive =false; AServicepointmanager.checkcertificaterevocationlist =true; -Servicepointmanager.defaultconnectionlimit = -; -Servicepointmanager.expect100continue =false; the -Request. Method ="POST"; -Request. ContentType ="multipart/form-data; boundary="+formdataboundary; -Request. Accept ="Application/json"; +Request. Referer =NULL; -Request. AllowAutoRedirect =true; +StringBuilder Builder =NewStringBuilder (); A at foreach(varDicinchparameters) - { -Builder. Appendline (string. Format ("--{0}\r\ncontent-disposition:form-data; name=\ "{1}\" \r\n\r\n{2}", - Formdataboundary, - DiC. Key, - DiC. Value)); in } -Builder. Appendline (string. Format ("--{0}--", Formdataboundary)); to +Formdatastream.write (encoding. GetBytes (builder. ToString ()),0, encoding. GetByteCount (builder. ToString ())); -Formdatastream.position =0; the byte[] FormData =New byte[formdatastream.length]; *Formdatastream.read (FormData,0, formdata.length); $ formdatastream.close ();Panax Notoginseng -Stream newstream =request. GetRequestStream (); theNewstream.write (FormData,0, formdata.length); + newstream.close (); A theHttpWebResponse response =(HttpWebResponse) request. GetResponse (); +Stream stream =Response. GetResponseStream (); - stringresult =string. Empty; $ using(StreamReader sr =NewStreamReader (stream)) $ { -result =Sr. ReadToEnd (); - } the Console.Write (result); -}
C # Docking HTTPS interface