C # analog Post submission Form (ii)--httpwebrequest and HttpWebResponse

Source: Internet
Author: User

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?
  1. HttpWebRequest request = (HttpWebRequest) webrequest.create ("Address of the POST request");
  2. Request.  Cookiecontainer = new Cookiecontainer ();
  3. Cookiecontainer cookie = Request. Cookiecontainer; //If you do not use cookies, delete them
  4. The following is the HTTP headers sent, casually add, which referer is very important, some sites will be based on this to anti-hotlinking
  5. Request.  Referer = "http://localhost/index.php";
  6. Request.  Accept = "accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  7. Request.  headers["accept-language"] = "zh-cn,zh;q=0.";
  8. Request.  headers["Accept-charset"] = "gbk,utf-8;q=0.7,*;q=0.3";
  9. 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 ";
  10. Request.  KeepAlive = true;
  11. The HTTP header above depends on the situation, but the following two must be added
  12. Request.  ContentType = "application/x-www-form-urlencoded";
  13. Request.  Method = "POST";
  14. Encoding Encoding = Encoding.UTF8; //Customized according to the coding of the website
  15. byte[] PostData = encoding. GetBytes (POSTDATASTR); //postdatastr is the data sent, the format is the same as the last said
  16. Request. ContentLength = Postdata.length;
  17. Stream requeststream = Request. GetRequestStream ();
  18. requestStream.Write (postdata, 0, postdata.length);
  19. HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
  20. Stream Responsestream = Response. GetResponseStream ();
  21. If the HTTP header accepts gzip, it is necessary to determine whether there is compression, if so, the direct decompression can be
  22. if (response. headers["content-encoding"] ! = NULL && response. headers["Content-encoding"]. ToLower (). Contains ("gzip"))
  23. {
  24. Responsestream = New GZipStream (Responsestream, compressionmode.decompress);
  25. }
  26. StreamReader StreamReader = new StreamReader (Responsestream, encoding);
  27. String retstring = Streamreader.readtoend ();
  28. Streamreader.close ();
  29. Responsestream.close ();
  30. 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

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.