C # simulate landing and continue to visit other pages

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Net;
Using System.Text;
Using System.Threading;

Namespace ConsoleApplication4
{

Class Program
{
static void Main (string[] args)
{
Testing the information below is available in F12.
Httpheader Header = new Httpheader ();
header.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
Header.contenttype = "application/x-www-form-urlencoded";//This F12 is not, this must be, this is the content type of the request header, otherwise the parameters can not pass through.
Header.method = "POST";
Header.useragent = "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/36.0.1985.125 safari/537.36 ";
Header.maxtry = 300;

String poststring = "loginname=admin&loginpwd=123456";//Here is the parameter passed, you can use the tool to grasp the packet analysis, but also can analyze itself, mainly in the form of each name should be added in

String html = htmlhelper.gethtml ("Http://localhost:12055/Admin/Home/MainForm", Htmlhelper.getcookie ("HTTP/ Localhost:12055/admin/login/checklogin ", poststring, header), header);

Console.WriteLine (HTML);

Console.readkey ();
}


public class HtmlHelper
{
//<summary>
//Get cookies
//</summary>
//<param Name= "Loginurl" ></PARAM>
//<param name= "PostData" ></PARAM>
//<param name= "header "></PARAM>
//<returns></returns>
public static Cookiecontainer GetCookie (string Loginurl, String postdata, Httpheader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Try
{
Cookiecontainer cc = new Cookiecontainer ();
request = (HttpWebRequest) webrequest.create (loginurl) ;
Request. Method = Header.method;
Request. ContentType = Header.contenttype;
byte[] Postdatabyte = Encoding.UTF8.GetBytes (postdata);
Request. ContentLength = Postdatabyte. Length;
Request. AllowAutoRedirect = false;
Request. Cookiecontainer = CC;
Request. KeepAlive = true;

Submit Request
Stream stream;
stream = Request. GetRequestStream ();
Stream. Write (postdatabyte, 0, Postdatabyte. Length);
Stream. Close ();

Receive response
Response = (HttpWebResponse) request. GetResponse ();
Response. Cookies = Request. Cookiecontainer.getcookies (Request. RequestUri);

Cookiecollection Cook = Response. Cookies;
Cookie string format
String strcrook = Request. Cookiecontainer.getcookieheader (Request. RequestUri);

return cc;
}
catch (Exception ex)
{

Throw ex;
}
}

<summary>
Get HTML
</summary>
<param name= "GetUrl" ></param>
<param name= "Cookiecontainer" ></param>
<param name= "Header" ></param>
<returns></returns>
public static string gethtml (String getUrl, Cookiecontainer Cookiecontainer,httpheader header)
{
Thread.Sleep (1000);
HttpWebRequest HttpWebRequest = null;
HttpWebResponse HttpWebResponse = null;
Try
{
HttpWebRequest = (HttpWebRequest) httpwebrequest.create (GETURL);
Httpwebrequest.cookiecontainer = Cookiecontainer;
Httpwebrequest.contenttype = Header.contenttype;
HttpWebRequest.ServicePoint.ConnectionLimit = Header.maxtry;
Httpwebrequest.referer = GETURL;
Httpwebrequest.accept = header.accept;
Httpwebrequest.useragent = header.useragent;
Httpwebrequest.method = "GET";
HttpWebResponse = (HttpWebResponse) httpwebrequest.getresponse ();
Stream responsestream = Httpwebresponse.getresponsestream ();
StreamReader StreamReader = new StreamReader (Responsestream, Encoding.UTF8);
String html = Streamreader.readtoend ();
Streamreader.close ();
Responsestream.close ();
Httpwebrequest.abort ();
Httpwebresponse.close ();
return HTML;
}
catch (Exception e)
{
if (HttpWebRequest! = null) Httpwebrequest.abort ();
if (HttpWebResponse! = null) httpwebresponse.close ();
return string. Empty;
}
}
}

public class Httpheader
{
public string ContentType {get; set;}

public string Accept {get; set;}

public string useragent {get; set;}

public string Method{get;set;}

public int Maxtry {get; set;}
}

}
}

C # simulate landing and continue to visit other pages

Related Article

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.