Huang Cong: C # simulate the form for submitting POST data on the website page (2)-HttpWebRequest and HttpWebResponse

Source: Internet
Author: User

The last time I introduced the use of WebClient to submit POST requests, this time I will continue to introduce other methods

HttpWebRequestAndHttpWebResponse

I think the biggest difference from the WebClient introduced last time is that HttpWebRequest is more flexible and powerful. For example, HttpWebRequest supportsCookieBut WebClient does not support it. Therefore, if you need to log on to a website to perform some operations, HttpWebResponse will be used.

Supplement:

WebClient can operate on cookies. Because cookies are essentially strings, as long as the header returned by the server is "SetCooie: xxx", process them in the returned format (they cannot be returned as is, for more information, see capture packets and analyze the format), save it, and add "Cookie: xxx" to the HTTP request header.

First, you must mention the Referer and Cookie.

Referer: it is the header information that is usually carried when an Http request is sent in a browser. It is widely used to count the click information, that is, from the click, so some websites will also use this attribute to refer to anti-leech, this principle is often used if any image is limited to internal communication.

Cookie: some websites store data stored on the user's local terminal to identify the user's identity and perform session tracking (usually encrypted). It is usually used when you log on. After logon, the website stores a Cookie on a local computer. Every time you visit the website, the Cookie of the website will be sent together, and the server will use it to confirm your identity. It is an important piece of information. Some hackers also steal cookies to intrude into your account.

Now, let's take a look:

HttpWebRequest request = (HttpWebRequest) WebRequest. create ("POST request address"); request. cookieContainer = new CookieContainer (); CookieContainer cookie = request. cookieContainer; // if no Cookie is used, delete it. // The following is the http header sent. Add it as needed. referer is very important. Some websites will anti-leech requests based on this. referer = "http: // localhost/index. php "; request. accept = "Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8"; request. headers ["Accept-La Nguage "] =" 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 above http header depends on the situation, but the following two must be added with request. contentType = "application/x-www-form-urlencoded"; request. method = "POST"; Encoding encoding = Encoding. UTF8; // depending on the website Encoding custom byte [] postData = encoding. getBytes (postDataStr); // postDataStr is the data sent, and the format is the same as the previous 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, You need to determine whether there is compression. if yes, extract it directly 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 that I just share my knowledge with you, not to let you do anything that damages others' websites.

In turn, as a Web developer, you must understand that you cannot believe that the data sent by the client is always legal, or that others can only access the website through a browser. The above is an example.

As a precaution, most people like verification codes can be prevented ~ However, do not think that everyone can be protected by the Verification Code. If you want to know what will happen later, please try again ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.