The principle of analog landing is very simple, is to send an HTTP request server to get the response, and then the client gets a cookie to achieve the analog landing, such as some of the ticket software is the principle of this simulation of the client's cookies and then send a request to grab tickets, then 12306 This article will demonstrate how to To achieve the analog landing, recommend a tool fiddler, this is a monitor HTTP request of a sharp weapon. Nonsense not much to say, I take blog Park as an example to achieve analog landing. First I log into the blog Park http://passport.cnblogs.com/login.aspx enter the username and password point login will see the relevant information on the Fiddler:
Ok, I first need to send an HTTP request, the way the post is requested, and then the username and password are the post data. The code is as follows:
static Cookiecontainer GetCookie (String poststring, String posturl) {Cookiecontaine
R cookie = new Cookiecontainer (); HttpWebRequest Httprequset = (HttpWebRequest) httpwebrequest.create (PostURL);//create HTTP request Httprequset.cookiecontainer
= cookie;//Set Cookie Httprequset.method = "POST";//post commit httprequset.keepalive = true; Httprequset.useragent = "mozilla/5.0 (Windows NT 6.3; WOW64; trident/7.0;
rv:11.0) like Gecko ";
Httprequset.accept = "text/html, Application/xhtml+xml, */*"; Httprequset.contenttype = "application/x-www-form-urlencoded";//The above information is copied directly over the time of the listening request byte[] bytes =
System.Text.Encoding.UTF8.GetBytes (poststring); httprequset.contentlength = bytes.
Length;
Stream stream = Httprequset.getrequeststream (); Stream. Write (bytes, 0, bytes.
Length); Stream. Close ();//above is the write HttpWebResponse HttpResponse = (HttpWebResponse) httprequset.getresponse () of the Post data.//Get the service-side response return cookie;//get cookies}
After we get the cookie, we can take what the user has to the backend or other places:
static string GetContent (Cookiecontainer cookie, string url)
{
string content;
HttpWebRequest HttpRequest = (HttpWebRequest) httpwebrequest.create (URL);
Httprequest.cookiecontainer = cookie;
httprequest.referer = URL;
Httprequest.useragent = "mozilla/5.0 (Windows NT 6.3; WOW64; trident/7.0; rv:11.0) Like Gecko ";
Httprequest.accept = "text/html, Application/xhtml+xml, */*";
Httprequest.contenttype = "application/x-www-form-urlencoded";
Httprequest.method = "Get";
HttpWebResponse HttpResponse = (HttpWebResponse) httprequest.getresponse ();
using (Stream Responsestream = Httpresponse.getresponsestream ())
{
using (StreamReader sr = new StreamReader ( Responsestream, System.Text.Encoding.UTF8)
{
content = Sr. ReadToEnd ();
}
return content;
}
OK Here is the call I wrote is a console program:
static void Main (string[] args)
{
string loginstr = "{The login data to post includes user name and password}";
Obtain a cookie
cookiecontainer cookie = GetCookie (loginstr, "http://passport.cnblogs.com/login.aspx") from the landing address;
This is to enter the background address
Console.WriteLine (getcontent (Cookie, "http://i.cnblogs.com/EditPosts.aspx"));
Console.read ();
}
I can see that I have entered the background:
If I am not logged in case access to this address is like this:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.