Home>

There are many uses for automatic web login (submitting post content).Such as identity verification, program upgrades, online voting, etc.Here's how to do it in c#.

The core of automatic webpage login and submission of post information is to analyze the source code (html) of the webpage. In C#, there are many components that can be used to extract the webpage html.The three commonly used are webbrowser, webclient, and httpwebrequest. These three methods are used to achieve the following:

1, webbrowser is a "mini" browser, which is characterized by no need to care about cookies, built-in JS and other issues

Webbrowser is a new component provided by vs2005 (in fact, it encapsulates the ie interface). The implementation of the post function is generally implemented by analyzing the htmldocument in the documentcompleted of webbrowser.

htmlelement clickbtn=null;
if (e.url.tostring (). tolower (). indexof ("xxx.htm")>0) //landing page
{
  htmldocument doc=webbrowser1.document;
  for (int i=0;i<doc.all.count;i ++)
  {
   if (doc.all [i] .tagname.toupper (). equals ("input"))
   {
    switch (doc.all [i] .name)
    {
     case "userctl":
      doc.all [i] .innertext="user01";
      break;
     case "passct1":
      doc.all [i] .innertext="mypass";
      break;
     case "b1":
      clickbtn=doc.all [i];//Submit button
      break;
    }
   }
  }
  clickbtn.invokemember ("click");//Perform a button operation
}

2, webclient encapsulates some classes of http,easy to use,Compared with webbrowser, the feature is that you can set your own proxy.The disadvantage is the control of cookies

webclient runs in the background,And provides the ability to operate asynchronously,This is very convenient for concurrent tasks,Then wait for the result to return,Then deal with them one by one.The multitasking asynchronous call code is as follows:

private void startloop (int proxynum)
{
 webclient [] wcarray=new webclient [proxynum];//Init
 for (int idarray=0;idarray<proxynum;idarray ++)
 {
  wcarray [idarray]=new webclient ();
  wcarray [idarray] .openreadcompleted +=new openreadcompletedeventhandler (pic_openreadcompleted2);
  wcarray [idarray] .uploaddatacompleted +=new uploaddatacompletedeventhandler (pic_uploaddatacompleted2);
  try
  {
   ...
   wcarray [idarray] .proxy=new webproxy (proxy [1], port);
   wcarray [idarray] .openreadasync (new uri ("http://xxxx.com.cn/tp.asp?id=129"));//Open the web;
   proxy=null;
  }
  catch
  {
  }
 }
}
private void pic_openreadcompleted2 (object sender, openreadcompletedeventargs e)
{
  if (e.error == null)
  {
     string textdata=new streamreader (e.result, encoding.default) .readtoend ();//Get the returned information
     .....
     string cookie=((webclient) sender) .responseheaders ["set-cookie"];
     ((webclient) sender) .headers.add ("content-type", "application/x-www-form-urlencoded");
     ((webclient) sender) .headers.add ("accept-language", "zh-cn");
     ((webclient) sender) .headers.add ("cookie", cookie);
     string postdata="..."
     byte [] bytearray=encoding.utf8.getbytes (postdata);//convert to binary array
     ((webclient) sender) .uploaddataasync (new uri ("http://xxxxxxy.com.cn/tp.asp?id=129"), "post", bytearray);
  }
}
private void pic_uploaddatacompleted2 (object sender, uploaddatacompletedeventargs e)
{
  if (e.error == null)
  {
   string returnmessage=encoding.default.getstring (e.result);
   ...
  }
}

3, httpwebrequest is relatively low-level,Can achieve more functions,cookie operation is also very simple

private bool postwebrequest ()
{
   cookiecontainer cc=new cookiecontainer ();
   string pos tdata="user =" + struser + "&pass =" + strpsd;
   byte [] bytearray=encoding.utf8.getbytes (postdata);//conversion
   httpwebrequest webrequest2=(httpwebrequest) webrequest.create (new uri ("http://www.xxxx.com/chk.asp"));
   webrequest2.cookiecontainer=cc;
   webrequest2.method="post";
   webrequest2.contenttype="application/x-www-form-urlencoded";
   webrequest2.contentlength=bytearray.length;
   stream newstream=webrequest2.getrequeststream ();
   //send the data.
   newstream.write (bytearray, 0, bytearray.length);//write parameters
   newstream.close ();
   httpwebresponse response2=(httpwebresponse) webrequest2.getresponse ();
   streamreader sr2=new streamreader (response2.getresponsestream (), encoding.default);
   string text2=sr2.readtoend ();
   ...
}

httpwebrequest also provides asynchronous operations,Interested friends check the msdn themselves, it is not difficult to achieve.

There are many uses for client programs to simulate post submissions.Often used for interface interaction between different platforms,

The landlord summed it up very well,But one less method:

webrequest request=webrequest.create (url);
request.method="post";
request.timeout=100000;
request.getrequeststream (). close ();
webresponse response=request.getresponse ();
streamreader sr=new streamreader (response.getresponsestream (), system.text.encoding.utf8);
webinfo=sr.readtoend ();
sr.close ();

Very convenient

c
  • Previous Introduction to Python's pycurl package usage
  • Next C # code for generating random numbers