Statement: I only in the spare time to write "Happy Net Auxiliary program", the purpose is only to learn!
Since there have been similar programs written before, also wrote related articles introduced (C # Web site Login Learning notes (i): Login simple Web site, C # Web site Login Learning notes (ii): Access to the page to be accessed after the login can be read, this time to write "Happy Net Auxiliary program" can be considered handy, Directly from the computer to turn out the dust-laden httphelper (the two articles mentioned above is in the operation of the Class), a little analysis of the Web page structure (for parking), to write the program!
Before you start writing notes, let's see what software is needed to write such a "plug-in" program?
1. Grab Kit: Http Analyzer V3. Since the HTTP simulation request is being implemented, the grab kit must be
2. Web Analytics Tools: Firefox 3.0 + Firebug 1.2.1. Yes, the lovely fire fox is helping again.
In this post, will simply introduce how to log into the net, to obtain the relevant data for the parking space.
One, slightly modified the code of the Httphelper class:
Code
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net;
Using System.IO;
Namespace Snshelper.common {class Httphelper {#region A private variable, privately cookiecontainer cc;
private string contentType = "application/x-www-form-urlencoded"; private string accept = "Image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/x-shockwave-flash, Application/x-silverlight, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, Application/x-ms-application, APPLICATION/X-MS-XBAP, Application/vnd.ms-xpsdocument, Application/xaml+xml,
APPLICATION/X-SILVERLIGHT-2-B1, */* "; private string useragent = "mozilla/4.0" (compatible; MSIE 7.0;
Windows NT 5.1. NET CLR 2.0.50727;. NET CLR 3.0.04506.648;. NET CLR 3.5.21022) ";
Private Encoding Encoding = encoding.getencoding ("Utf-8"); #endregion #region Properties///<summary>///cookie container///</summary> public Cookiecontainer
Cookiecontainer { get {return cc; }///<summary>///the encoding used to get the source of the Web page///</summary>///<value></value> p
Ublic Encoding Encoding {get {return Encoding;
} set {encoding = value; #endregion #region Constructor///<summary>///Initializes a new instance of the <see cref=
"Httphelper"/> class.
</summary> public Httphelper () {cc = new Cookiecontainer ();
}///<summary>///Initializes a new instance of the <see cref= "Httphelper" class.
</summary>///<param name= "CC" >the cc.</param> public Httphelper (Cookiecontainer cc) {
this.cc = CC;
}///<summary>///Initializes a new instance of the <see cref= "Httphelper" class. </summary>///<param name= "ContentType" >type of the CONTENT.</PARAM> <param name= "Accept" >the accept.</param>///<param name= "useragent" >the user AGENT.</PARAM&G
T
Public Httphelper (String contentType, String accept, String useragent) {this.contenttype = ContentType;
This.accept = accept;
This.useragent = useragent;
}///<summary>///Initializes a new instance of the <see cref= "Httphelper" class. </summary>///<param name= "cc" >the cc.</param>///<param name= "ContentType" >type of The content.</param>///<param name= "Accept" >the accept.</param>///<param "Name=" >the User agent.</param> public Httphelper (Cookiecontainer cc, string contentType, string accept, String user
Agent) {this.cc = cc;
This.contenttype = ContentType;
This.accept = accept;
This.useragent = useragent; #endregion #region Public methods///<summary>///Get the HTML code for the specified page///</summary>///<param name= "url" > The path of the specified page </param>///<param name= "Pos Tdata "> Postback data </param>///<param name=" IsPost "> whether to send the request on post </param>///<param name=" Cooki Ecollection ">cookie collection </param>///<returns></returns> public string gethtml (string URL, Strin G PostData, bool IsPost, Cookiecontainer Cookiecontainer) {if (string.
IsNullOrEmpty (PostData)) {return gethtml (URL, cookiecontainer);
} byte[] Byterequest = Encoding.Default.GetBytes (postdata);
HttpWebRequest HttpWebRequest;
HttpWebRequest = (HttpWebRequest) httpwebrequest.create (URL);
Httpwebrequest.cookiecontainer = Cookiecontainer;
Httpwebrequest.contenttype = ContentType;
Httpwebrequest.referer = URL;
Httpwebrequest.accept = Accept;
Httpwebrequest.useragent = useragent; Httpwebrequest.method = IsPost?
"POST": "Get"; HttpweBrequest.contentlength = Byterequest.length;
Stream stream = Httpwebrequest.getrequeststream (); Stream.
Write (byterequest, 0, byterequest.length); Stream.
Close ();
HttpWebResponse HttpWebResponse;
HttpWebResponse = (HttpWebResponse) httpwebrequest.getresponse ();
Stream responsestream = Httpwebresponse.getresponsestream ();
StreamReader StreamReader = new StreamReader (responsestream, encoding);
String html = Streamreader.readtoend ();
Streamreader.close ();
Responsestream.close ();
return HTML; ///<summary>///Gets the HTML code for the specified page///</summary>///<param name= "url" > The path of the specified page </pa ram>///<param name= "cookiecollection" >cookie set </param>///<returns></returns> PU
Blic string gethtml (string url, Cookiecontainer cookiecontainer) {HttpWebRequest HttpWebRequest;
HttpWebRequest = (HttpWebRequest) httpwebrequest.create (URL); HTtpwebrequest.cookiecontainer = Cookiecontainer;
Httpwebrequest.contenttype = ContentType;
Httpwebrequest.referer = URL;
Httpwebrequest.accept = Accept;
Httpwebrequest.useragent = useragent;
Httpwebrequest.method = "Get";
HttpWebResponse HttpWebResponse;
HttpWebResponse = (HttpWebResponse) httpwebrequest.getresponse ();
Stream responsestream = Httpwebresponse.getresponsestream ();
StreamReader StreamReader = new StreamReader (responsestream, encoding);
String html = Streamreader.readtoend ();
Streamreader.close ();
Responsestream.close ();
return HTML; ///<summary>///Gets the HTML code for the specified page///</summary>///<param name= "url" > The path of the specified page </param
>///<returns></returns> public string gethtml (string url) {return gethtml (URL, cc); ///<summary>///Gets the HTML code for the specified page///</summary>///<param name= "url" > The path of the specified pageDia </param>///<param name= "PostData" > Postback data </param>///<param name= "IsPost" > whether to send a request by post
lt;/param>///<returns></returns> public string gethtml (string url, string postdata, bool isPost)
{return gethtml (URL, postdata, isPost, CC); } #endregion}}