天天看点

12306.cn网站自动登录器源代码

去年过年放假的时候写了一个12306.cn网站的自动登录器,刚好那时候放假了,所以没把源代码放出来,现在将代码发出来,由于编写得比较仓促(从放假的下午19:00左右到晚上到00:00左右),很多细节问题考虑不是很全面,如断网的情况未考虑,界面比较简单,错误之处请大家批评指正.

注意:本程序基于.net framework4.0编写,UI使用WPF

源代码下载地址1:

http://115.com/file/e7l5t4uw 源代码下载地址2: http://files.cnblogs.com/loyldg/AutoLogin12306_src.rar

其实自动登录比较简单,就是发送一个http请求并传递参数,然后对响应的结果进行分析处理.下面我列出需要注意的几个问题.           

1.发送http请求时,需要对传递的参数进行Url编码(使用HttpUtility.UrlEncode方法)

2.刷新验证码后,需要将当前的Cookie信息替换(刷新验证码后会产生的新的Cookie,使用此Cookie替换原有Cookie信息即可)

3.处理Https证书问题,直接信任所有证书即可.

复制代码

private  void SetCertificatePolicy()         {             ServicePointManager.ServerCertificateValidationCallback                        += RemoteCertificateValidate;         }         private  bool RemoteCertificateValidate(            object sender, X509Certificate cert,             X509Chain chain, SslPolicyErrors error)         {             return true;         }           

4.登录成功后,打开IE浏览器,并将登录成功后的Cookie信息传递到IE浏览器,这里需要使用到InternetSetCookie方法,该方法在wininet.dll中.

关键代码如下:

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

public static extern bool InternetSetCookie(string lpszUrl, string lbszCookieName, string lpszCookieData);           

private void OpenIEWithCookie(CookieCollection cookies,string url)

{             string expires = "expires=Sun,22-Feb-2099 00:00:00 GMT";                         foreach (Cookie item in cookies)             {                bool isSuccess= InternetSetCookie(item.Path, item.Name, string.Format("{0};{1}",item.Value,expires));                if (!isSuccess)                {                    int errorCode = Marshal.GetLastWin32Error();                    App.Messenger.NotifyColleagues(Msg.APPEND_MESSAGE, "set cookie error,errorCode:"+errorCode);                }             }             string ieFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Internet Explorer\\iexplore.exe";             if (File.Exists(ieFilePath))             {                 System.Diagnostics.Process.Start(ieFilePath, url);                 App.Messenger.NotifyColleagues(Msg.APPEND_MESSAGE, "Open Url:" + url);             }             else             {                 App.Messenger.NotifyColleagues(Msg.APPEND_MESSAGE, "未找到IE浏览器");             }         }