C # Post information to a website and obtain Cookies

Source: Internet
Author: User

Using System. Net;
Using System. IO;
Using System. Text;
[STAThread]
Static void Main (string [] args)
{

String url = "http://hi.baidu.com/yimeng3025/test.asp ";

String indata = "aa = zhuye ";
String outdata = "";
CookieContainer myCookieContainer = new CookieContainer ();
String 8

// Create a CookieContainer to store the Cookie set
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest. Create (url );
// Create an HttpWebRequest
MyHttpWebRequest. ContentType = "application/x-www-form-urlencoded ";
MyHttpWebRequest. ContentLength = indata. Length;
MyHttpWebRequest. Method = "POST ";
MyHttpWebRequest. CookieContainer = myCookieContainer;
// Set CookieContainer of HttpWebRequest to the myCookieContainer just created
Stream myRequestStream = myHttpWebRequest. GetRequestStream ();
StreamWriter myStreamWriter = new StreamWriter (myRequestStream, Encoding. GetEncoding ("gb2312 "));
MyStreamWriter. Write (indata );
// Write data to the Request stream of HttpWebRequest
MyStreamWriter. Close ();
MyRequestStream. Close ();
// Close the open object
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest. GetResponse ();
String 9

// Create an HttpWebResponse
MyHttpWebResponse. Cookies = myCookieContainer. GetCookies (myHttpWebRequest. RequestUri );
// Obtain the CookieCollection of a Cookie set containing a url
Stream myResponseStream = myHttpWebResponse. GetResponseStream ();
StreamReader myStreamReader = new StreamReader (myResponseStream, Encoding. GetEncoding ("gb2312 "));
Outdata = myStreamReader. ReadToEnd ();
// Read the data from the Response stream of HttpWebResponse
MyStreamReader. Close ();
MyResponseStream. Close ();
Console. WriteLine (outdata );
// Display "Logon"

// Get the Cookie and then perform the request to directly read the Post-Logon content.
MyHttpWebRequest = (HttpWebRequest) WebRequest. Create (url );
MyHttpWebRequest. CookieContainer = myCookieContainer ;//*
// The CookieContainer that just now has a Cookie, which can be directly verified by attaching it to HttpWebRequest.
MyHttpWebResponse = (HttpWebResponse) myHttpWebRequest. GetResponse ();
String 1

MyHttpWebResponse. Cookies = myCookieContainer. GetCookies (myHttpWebRequest. RequestUri );
MyResponseStream = myHttpWebResponse. GetResponseStream ();
MyStreamReader = new StreamReader (myResponseStream, Encoding. GetEncoding ("gb2312 "));
Outdata = myStreamReader. ReadToEnd ();
MyStreamReader. Close ();
MyResponseStream. Close ();
Console. WriteLine (outdata );
// Display "Logon" again"
// If you annotate the line *, it will display "No Logon"
}

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.