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浏览器");
        }
    }

复制代码
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值