HTTP defines the different methods of interacting with the server, the basic methods are Get,post,put,delete, respectively, for the search, that, increase, delete. In general we only use Get and post, the other two can also be implemented with Get and post, many browsers have only implemented the get and post two ways
Get: User gets information
Safe: Does not modify the data on the server, and destroys
Idempotent: Same request returns the same result
The parameters are transmitted by URL, followed by the middle of the URL. Separate, with & separate between the different parameters, the following means passing two parameters param1 and param2, with values of value1 and value2
Http://songurl/index.htm?param1=value1¶m2=value2
As can be seen above, get request data in privacy is not safe, because the information on the URL can be directly visible to users, which is not conducive to privacy protection
POST: for updating data
The data passed to the user is hidden, encapsulated in the package, the concrete look at the demo
Sometimes the request redirects, but we need to get something from the redirect URL, you can set redirection disable, you can get the redirect address from the Headers Location property
Request. AllowAutoRedirect = false;
String[] values = Request. Headers.getvalues ("location");
The following is the definition of the helper class
Public classHttphelper {/// <summary> ///Create a Get method HTTP request/// </summary> Public StaticHttpWebResponse Creategethttpresponse (stringUrlintTimeoutstringuseragent, cookiecollection cookies) {HttpWebRequest Request=NULL; if(URL.) StartsWith ("HTTPS", StringComparison.OrdinalIgnoreCase)) { //Validation of server-side certificates (certificates issued by non-third-party authorities, such as those generated by themselves, not verified, return true here)Servicepointmanager.servercertificatevalidationcallback =NewRemotecertificatevalidationcallback (CheckValidationResult); Request= WebRequest.Create (URL) asHttpWebRequest; Request. ProtocolVersion= Httpversion.version10;//HTTP version, default is 1.1, here is set to 1.0 } Else{Request= WebRequest.Create (URL) asHttpWebRequest; } request. Method="GET"; //set proxy useragent and timeouts//request. useragent = useragent; //request. Timeout = timeout; if(Cookies! =NULL) {request. Cookiecontainer=NewCookiecontainer (); Request. Cookiecontainer.add (cookies); } returnRequest. GetResponse () asHttpWebResponse; } /// <summary> ///Create a post-mode HTTP request/// </summary> Public StaticHttpWebResponse Createposthttpresponse (stringURL, idictionary<string,string> Parameters,intTimeoutstringuseragent, cookiecollection cookies) {HttpWebRequest Request=NULL; //If you are sending an HTTPS request if(URL.) StartsWith ("HTTPS", StringComparison.OrdinalIgnoreCase)) { //servicepointmanager.servercertificatevalidationcallback = new Remotecertificatevalidationcallback ( CheckValidationResult);Request = WebRequest.Create (URL) asHttpWebRequest; //request. ProtocolVersion = Httpversion.version10; } Else{Request= WebRequest.Create (URL) asHttpWebRequest; } request. Method="POST"; Request. ContentType="application/x-www-form-urlencoded"; //set proxy useragent and timeouts//request. useragent = useragent; //request. Timeout = timeout; if(Cookies! =NULL) {request. Cookiecontainer=NewCookiecontainer (); Request. Cookiecontainer.add (cookies); } //Send 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 =Encoding.ASCII.GetBytes (buffer. ToString ()); using(Stream stream =request. GetRequestStream ()) {stream. Write (data,0, data. Length); } } string[] values = Request. Headers.getvalues ("Content-type"); returnRequest. GetResponse () asHttpWebResponse; } /// <summary> ///get the requested data/// </summary> Public Static stringgetresponsestring (HttpWebResponse webresponse) {using(Stream s =WebResponse. GetResponseStream ()) {StreamReader Reader=NewStreamReader (S, Encoding.UTF8); returnReader. ReadToEnd (); } } /// <summary> ///Verifying Certificates/// </summary> Private Static BOOLCheckValidationResult (Objectsender, X509Certificate certificate, X509chain chain, sslpolicyerrors errors) { if(Errors = =Sslpolicyerrors.none)return true; return false; } }
"C #" HTTP request Get,post