usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net.Security;usingSystem.Security.Cryptography.X509Certificates;usingSystem.Net;usingSystem.IO;usingSystem.IO.Compression;usingSystem.Text.RegularExpressions; namespacehttpwebrequestdemo{classProgram {Private Static ReadOnly stringDefaultuseragent ="mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. NET CLR 1.1.4322;. NET CLR 2.0.50727)"; Private Static BOOLCheckValidationResult (Objectsender, X509Certificate certificate, X509chain chain, sslpolicyerrors errors) { return true;//Always accept } Public StaticHttpWebResponse Createposthttpresponse (stringURL, idictionary<string,string>parameters,encoding CharSet) {HttpWebRequest Request=NULL; //HTTPSQ RequestServicepointmanager.servercertificatevalidationcallback =NewRemotecertificatevalidationcallback (CheckValidationResult); Request= WebRequest.Create (URL) asHttpWebRequest; Request. ProtocolVersion=Httpversion.version10; Request. Method="POST"; Request. ContentType="application/x-www-form-urlencoded"; Request. UserAgent=defaultuseragent; //If you need post data if(! (Parameters = =NULL|| Parameters. Count = =0) {StringBuilder buffer=NewStringBuilder (); inti =0; foreach(stringKeyinchparameters. Keys) {if(I >0) {buffer. AppendFormat ("&{0}={1}", Key, Parameters[key]); } Else{buffer. AppendFormat ("{0}={1}", Key, Parameters[key]); } I++; } byte[] data =CharSet. GetBytes (buffer. ToString ()); using(Stream stream =request. GetRequestStream ()) {stream. Write (data,0, data. Length); } } returnRequest. GetResponse () asHttpWebResponse; } Static voidMain (string[] args) { stringURL ="https://192.168.1.101/httpOrg/create"; Encoding Encoding= Encoding.GetEncoding ("Utf-8"); IDictionary<string,string> Parameters =Newdictionary<string,string>(); Parameters. ADD ("Authuser","*****"); Parameters. ADD ("Authpass","*****"); Parameters. ADD ("Orgkey","*****"); Parameters. ADD ("OrgName","*****"); HttpWebResponse Response=Program.createposthttpresponse (url,parameters,encoding); //Print return valueStream stream = Response. GetResponseStream ();//gets the string stream of the responseStreamReader sr =NewStreamReader (stream);//Create a stream to read the stream stringhtml = Sr. ReadToEnd ();//read from the beginning to the end, put in the string HTMLConsole.WriteLine (HTML); } }}
HttpWebRequest is connected by default with proxies, resulting in slower results. The workaround is to configure:
Request. Proxy = null;
You can do this without using a proxy.
Example of C # using HttpWebRequest for Post requests (HTTPS)