zoukankan      html  css  js  c++  java
  • 12306.cn网站自动登录器源代码

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

     
    注意:本程序基于.net framework4.0编写,UI使用WPF
     
    源代码下载地址1:http://115.com/file/e7l5t4uw
     
            其实自动登录比较简单,就是发送一个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浏览器");
    }
    }


     

    作者:loyldg
    出处:http://www.cnblogs.com/loyldg/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。如有问题,可以邮件:loyldg@126.com  联系我,非常感谢。

  • 相关阅读:
    MySQL数据库之索引
    python面向对象编程
    linux端口占用
    lintcode
    java基础
    lintcode
    linux 常用命令
    Docker & Kubernates
    angular preparation
    java 命令行
  • 原文地址:https://www.cnblogs.com/loyldg/p/2340643.html
Copyright © 2011-2022 走看看