The last time I introduced a POST request in WebClient, I went on to introduce another way
HttpWebRequest and HttpWebResponse
The biggest difference from the last introduced WebClient is that HttpWebRequest is more flexible and powerful, for example, HttpWebRequest supports cookies, and WebClient does not, so HttpWebResponse comes in handy if you want to sign in to a website to do something.
Add:
WebClient is able to manipulate cookies, because cookies are essentially strings, as long as the server returns the header is "setcooie:xxx", so in the returned format to do the next processing (cannot return as is, specifically can be captured packet analysis under the format), Save up, Then add "cookie:xxx" to the HTTP request header.
The first thing to do is to mention referer and cookies.
Referer: is generally in the browser to send HTTP requests with the header information, is widely used to count click Information, that is, from that click, so some sites will also use this nature to the anti-theft chain, many times if what the picture only internal communication, such as, is the use of this principle.
Cookies: Some websites store data (usually encrypted) on the user's local terminal in order to identify the user, perform session tracking, and usually use it when you log in, and after logging in, the site stores a cookie for something on the local computer, and then every time you visit the website, The cookie of this website will be sent in the past, the server will rely on this to confirm your identity. It's an important message, and some hackers can hack into your account by stealing cookies.
Well, let's start by specifying:
[CSharp]View Plaincopyprint?
- HttpWebRequest request = (HttpWebRequest) webrequest.create ("Address of the POST request");
- Request. Cookiecontainer = new Cookiecontainer ();
- Cookiecontainer cookie = Request. Cookiecontainer; //If you do not use cookies, delete them
- The following is the HTTP headers sent, casually add, which referer is very important, some sites will be based on this to anti-hotlinking
- Request. Referer = "http://localhost/index.php";
- Request. Accept = "accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
- Request. headers["accept-language"] = "zh-cn,zh;q=0.";
- Request. headers["Accept-charset"] = "gbk,utf-8;q=0.7,*;q=0.3";
- Request. useragent = "user-agent:mozilla/5.0 (Windows NT 5.1) applewebkit/535.1 (khtml, like Gecko) chrome/14.0.835.202 Safari /535.1 ";
- Request. KeepAlive = true;
- The HTTP header above depends on the situation, but the following two must be added
- Request. ContentType = "application/x-www-form-urlencoded";
- Request. Method = "POST";
- Encoding Encoding = Encoding.UTF8; //Customized according to the coding of the website
- byte[] PostData = encoding. GetBytes (POSTDATASTR); //postdatastr is the data sent, the format is the same as the last said
- Request. ContentLength = Postdata.length;
- Stream requeststream = Request. GetRequestStream ();
- requestStream.Write (postdata, 0, postdata.length);
- HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
- Stream Responsestream = Response. GetResponseStream ();
- If the HTTP header accepts gzip, it is necessary to determine whether there is compression, if so, the direct decompression can be
- if (response. headers["content-encoding"] ! = NULL && response. headers["Content-encoding"]. ToLower (). Contains ("gzip"))
- {
- Responsestream = New GZipStream (Responsestream, compressionmode.decompress);
- }
- StreamReader StreamReader = new StreamReader (Responsestream, encoding);
- String retstring = Streamreader.readtoend ();
- Streamreader.close ();
- Responsestream.close ();
- return retstring;
Of course, please note, I just share the knowledge with you, not let everyone do harm to other people's website.
In turn, as a web developer, it is also understood that it is not possible to believe that the data sent by the client is always legal, and that others can only access the site through a browser, which is an example
And as a precaution, verification code and the like can prevent most people ~ Just, do not think that there is a verification code can prevent everyone, to know how to funeral, please listen to tell ~
C # analog Post submission Form (ii)--httpwebrequest and HttpWebResponse