您的位置:首页 > 运维架构 > 网站架构

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

2012-02-06 22:33 435 查看
去年过年放假的时候写了一个12306.cn网站的自动登录器,刚好那时候放假了,所以没把源代码放出来,现在将代码发出来,由于编写得比较仓促(从放假的下午19:00左右到晚上到00:00左右),很多细节问题考虑不是很全面,如断网的情况未考虑,界面比较简单,错误之处请大家批评指正.注意:本程序基于.netframework4.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证书问题,直接信任所有证书即可.
privatevoidSetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback
+=RemoteCertificateValidate;
}privateboolRemoteCertificateValidate(
objectsender,X509Certificatecert,
X509Chainchain,SslPolicyErrorserror)
{
returntrue;
}

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

关键代码如下:
[DllImport("wininet.dll",CharSet=CharSet.Auto,SetLastError=true)]
publicstaticexternboolInternetSetCookie(stringlpszUrl,stringlbszCookieName,stringlpszCookieData);privatevoidOpenIEWithCookie(CookieCollectioncookies,stringurl)
{
stringexpires="expires=Sun,22-Feb-209900:00:00GMT";
foreach(Cookieitemincookies)
{
boolisSuccess=InternetSetCookie(item.Path,item.Name,string.Format("{0};{1}",item.Value,expires));
if(!isSuccess)
{
interrorCode=Marshal.GetLastWin32Error();
App.Messenger.NotifyColleagues(Msg.APPEND_MESSAGE,"setcookieerror,errorCode:"+errorCode);
}
}
stringieFilePath=Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)+"\\InternetExplorer\\iexplore.exe";
if(File.Exists(ieFilePath))
{
System.Diagnostics.Process.Start(ieFilePath,url);
App.Messenger.NotifyColleagues(Msg.APPEND_MESSAGE,"OpenUrl:"+url);
}
else
{
App.Messenger.NotifyColleagues(Msg.APPEND_MESSAGE,"未找到IE浏览器");
}
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: