打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Wininet模拟登陆网站
分类: web开发 2014-02-14 14:52 555人阅读 评论(0) 收藏 举报

  现在事情比较多,多到不知道干什么好。抽点时间讲前一段时间学的的东西总结下,并和大家分享,主要用wininet模拟登录网站,供需要的朋友参考。

  1:自己抓包分析。我是在火狐浏览器上装了个插件(Live HTTP Fiddler都可以),就能看到看到登录网站和登陆后的一系列操作,所有的数据都在里面。

  2:就是模拟浏览器的发送请求。

  Windows下用wininet比较方便,用socket也可以(如果是https的网站,还要加安全机制,我用了openssl),可以参考我前面写的代码。Wininet的一些资料请访问下面的地址。

  http://download.csdn.net/detail/weiwei2012start/6922457

 3:下面的我的原始代码,可能有些地方并不需要,写的有点繁琐,请大家见谅。我用的是 这个网站 oschina.net。不多说,看代码。

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include <stdio.h>  
  3. #include <tchar.h>  
  4. #include <Windows.h>     
  5. #include <wininet.h>   
  6.   
  7. #pragma comment(lib,"wininet.lib")   
  8. static char Buff[102400];           //开辟100K的缓冲区  
  9.   
  10. DWORD dwOpenRequestFlags=   INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |    
  11.     INTERNET_FLAG_KEEP_CONNECTION |    
  12.     INTERNET_FLAG_PRAGMA_NOCACHE |  
  13.     INTERNET_FLAG_NO_AUTH |    
  14.     INTERNET_FLAG_RESYNCHRONIZE |  
  15.     //设置启用HTTPS     
  16.     INTERNET_FLAG_SECURE |    
  17.     INTERNET_FLAG_RELOAD;    
  18. /* 
  19. INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |   
  20. INTERNET_FLAG_KEEP_CONNECTION |   
  21. INTERNET_FLAG_NO_AUTH |   
  22. INTERNET_FLAG_NO_COOKIES |   
  23. INTERNET_FLAG_NO_UI |   
  24. //设置启用HTTPS    
  25. INTERNET_FLAG_SECURE |   
  26. INTERNET_FLAG_RELOAD;*/  
  27.   
  28.   
  29. //字符串到unicode码转换  
  30. //入口参数:待转换字符串,返回转换后的unicode指针  
  31. TCHAR* ANSI_To_Unicode(char *str )  
  32. {  
  33.     static TCHAR buffer[4096];  
  34.     memset(buffer,0,sizeof(buffer));    
  35.     MultiByteToWideChar(   
  36.         CP_ACP,                       
  37.         0,                        
  38.         str,                          
  39.         -1,                       
  40.         buffer,                       
  41.         strlen(str)   
  42.         );    
  43.     return  (TCHAR *)buffer;    
  44. }  
  45.   
  46. //初始化Internet  
  47. static HINTERNET Init_Internet()  
  48. {  
  49.     HINTERNET hOpenHandle;  
  50.     LPCTSTR lpszAgent = L"Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0";    
  51.     hOpenHandle = InternetOpen(  
  52.         lpszAgent,            
  53.         INTERNET_OPEN_TYPE_PRECONFIG,   
  54.         NULL,   
  55.         NULL,   
  56.         0  
  57.         );   
  58.     if(hOpenHandle == NULL)  
  59.     {  
  60.         printf("错误代码:%d\n",GetLastError());  
  61.     }  
  62.     return hOpenHandle;   
  63. }  
  64.   
  65. //注销Internet句柄  
  66. static int Close_Internet(HINTERNET hInternet)  
  67. {  
  68.     BOOL Stats = InternetCloseHandle( hInternet);  
  69.     if(Stats == FALSE)  
  70.     {  
  71.         printf("错误代码:%d",GetLastError());  
  72.         return 1;  
  73.     }  
  74.     return 0;  
  75. }  
  76.   
  77. //设置internet连接句柄 适用于http  
  78. //入口参数:网址、internetOpen 句柄  
  79. static HINTERNET  Set_Connect_handel(char *serverName,HINTERNET hOpenHandle){  
  80.     HINTERNET hConnectHandle;  
  81.     TCHAR *internetAddress = ANSI_To_Unicode(serverName);  
  82.     hConnectHandle = InternetConnect(  
  83.         hOpenHandle,                    // InternetOpen handle    
  84.         internetAddress,                // Server  name  
  85.         INTERNET_DEFAULT_HTTP_PORT,     // port  
  86.         NULL,                           // User name  
  87.         NULL,                           // User password  
  88.         INTERNET_SERVICE_HTTP,          // Service  
  89.         0,                              // Flags  
  90.         0                               // Context  
  91.         );  
  92.     if(hConnectHandle == NULL)  
  93.     {  
  94.         printf("错误代码:%d",GetLastError());  
  95.     }  
  96.     return hConnectHandle;        
  97. }  
  98.   
  99.   
  100. //设置internet连接句柄 适用于https  
  101. //入口参数:网址、internetOpen 句柄  
  102. static HINTERNET  Set_Https_Connect_handel(char *serverName,HINTERNET hOpenHandle){  
  103.     HINTERNET hConnectHandle;  
  104.     TCHAR *internetAddress = ANSI_To_Unicode(serverName);  
  105.     hConnectHandle = InternetConnect(  
  106.         hOpenHandle,                    // InternetOpen handle    
  107.         internetAddress,                // Server  name  
  108.         INTERNET_DEFAULT_HTTPS_PORT,    // Default HTTPS port - 443  
  109.         NULL,                           // User name  
  110.         NULL,                           // User password  
  111.         INTERNET_SERVICE_HTTP,          // Service  
  112.         0,                              // Flags  
  113.         0                               // Context  
  114.         );  
  115.   
  116.     if(hConnectHandle == NULL)  
  117.     {  
  118.         printf("错误代码:%d",GetLastError());  
  119.     }  
  120.     return hConnectHandle;        
  121. }  
  122.   
  123. //设置请求报文适用于http  
  124.   
  125. static HINTERNET Set_Request_Command_Http(HINTERNET hConnectHandle){  
  126.     HINTERNET hResourceHandle;  
  127.     hResourceHandle = HttpOpenRequest(  
  128.         hConnectHandle,                     // InternetConnect handle  
  129.         TEXT("GET"),                        // Method        
  130.         TEXT(""),                           // Object name    
  131.         NULL,                               // Version  
  132.         NULL,                               // Referrer  
  133.         NULL,                               // Extra headers       
  134.         INTERNET_FLAG_KEEP_CONNECTION,      // Flags   
  135.         0                                   // Context  
  136.         );  
  137.     if(hResourceHandle == NULL)  
  138.     {  
  139.         printf("错误代码:%d",GetLastError());  
  140.     }  
  141.     return hResourceHandle;  
  142. }  
  143.   
  144. static HINTERNET Set_Https_Request2(HINTERNET hConnectHandle,DWORD dwFlage){  
  145.     HINTERNET hResourceHandle;  
  146.     hResourceHandle = HttpOpenRequest(  
  147.         hConnectHandle,                     // InternetConnect handle  
  148.         L"POST",                                // Method        
  149.         L"/action/user/hash_login"  ,       // Object name        
  150.         L"HTTP/1.1",                                // Version  
  151.         L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL,                             // Referrer  
  152.         NULL,                               // Extra headers       
  153.         dwFlage,                    // Flags   
  154.         0                                   // Context  
  155.         );  
  156.     if(hResourceHandle == NULL)  
  157.     {  
  158.         printf("错误代码:%d",GetLastError());  
  159.     }  
  160.     return hResourceHandle;  
  161. }  
  162.   
  163. static HINTERNET Set_Https_Request3(HINTERNET hConnectHandle,DWORD dwFlage){  
  164.     HINTERNET hResourceHandle;  
  165.     hResourceHandle = HttpOpenRequest(  
  166.         hConnectHandle,                     // InternetConnect handle  
  167.         L"GET",                             // Method        
  168.         NULL,       // Object name        
  169.         L"HTTP/1.1",                                // Version  
  170.         NULL,//L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL,                              // Referrer  
  171.         NULL,                               // Extra headers       
  172.         dwFlage,                    // Flags   
  173.         0                                   // Context  
  174.         );  
  175.     if(hResourceHandle == NULL)  
  176.     {  
  177.         printf("错误代码:%d",GetLastError());  
  178.     }  
  179.     return hResourceHandle;  
  180. }  
  181.   
  182. static HINTERNET Set_Https_Request_Command2post(HINTERNET hConnectHandle){  
  183.     HINTERNET hResourceHandle;  
  184.     DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |    
  185.         INTERNET_FLAG_KEEP_CONNECTION |    
  186.         INTERNET_FLAG_NO_AUTH |    
  187.         INTERNET_FLAG_NO_COOKIES |    
  188.         INTERNET_FLAG_NO_UI |    
  189.         //设置启用HTTPS     
  190.         INTERNET_FLAG_SECURE |    
  191.         INTERNET_FLAG_RELOAD;    
  192.   
  193.     hResourceHandle = HttpOpenRequest(  
  194.         hConnectHandle,                     // InternetConnect handle  
  195.         L"GET",                             // Method        
  196.         L"/action/user/hash_login"  ,       // Object name        
  197.         L"HTTP/1.1",                                // Version  
  198.         L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL,                             // Referrer  
  199.         NULL,                               // Extra headers       
  200.         dwOpenRequestFlags,                 // Flags   
  201.         0                                   // Context  
  202.         );  
  203.     if(hResourceHandle == NULL)  
  204.     {  
  205.         printf("错误代码:%d",GetLastError());  
  206.     }  
  207.     return hResourceHandle;  
  208. }  
  209.   
  210.   
  211.   
  212. static int Send_Request2(HINTERNET hResourceHandle){  
  213.     BOOL stats;  
  214.     //char postData[1000={0};  
  215.     // LPCSTR  lpszHeaders = "Accept    */*\r\nAccept-Language  zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\nContent-Type: application/x-www-form-urlencoded\r\nX-Requested-With  XMLHttpRequest\r\nConnection    keep-alive\r\n\r\n";  //\r\nContent-Type    application/x-www-form-urlencoded; charset=ASCII  
  216.     LPCSTR  lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";    
  217.     DWORD   dwHeadersLength=-1L;    
  218.   
  219.       
  220.   
  221.     char szFormData[1024]={0};  
  222.     strcpy(szFormData,"email=xin&pwd=1411678a0b9e25ee2f7c8b2f7ac92b6a74b3f9c5&save_login=1");  
  223.     LPVOID pBuf=(LPVOID)szFormData;  
  224.     stats = HttpSendRequestA(hResourceHandle,lpszHeaders,dwHeadersLength,pBuf,strlen(szFormData));    
  225.     /*  
  226.     stats = HttpSendRequest(  
  227.     hResourceHandle,    //Handle returned by HttpOpenRequest or InternetOpenUrl  
  228.     lpszHeaders,   
  229.     dwHeadersLength,   
  230.     pBuf,   
  231.     strlen(szFormData)  
  232.     ); */  
  233.     return stats;  
  234. }  
  235.   
  236. static int Send_Request3(HINTERNET hResourceHandle){  
  237.     BOOL stats;  
  238.   
  239.     LPCSTR  lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";    
  240.     DWORD   dwHeadersLength=-1L;    
  241.   
  242.     stats = HttpSendRequest(  
  243.         hResourceHandle,    //Handle returned by HttpOpenRequest or InternetOpenUrl  
  244.         NULL,   
  245.         NULL,   
  246.         NULL,   
  247.         NULL  
  248.         );   
  249.     return stats;  
  250. }  
  251.   
  252. //接收报文头  
  253. static int Receive_Header(HINTERNET hResourceHandle,FILE *fp){  
  254.     DWORD dwInfoBufferLength;    
  255.     BOOL reslut = HttpQueryInfo(          
  256.         hResourceHandle,            //Handle returned by HttpOpenRequest or InternetOpenUrl  
  257.         HTTP_QUERY_RAW_HEADERS_CRLF,  
  258.         Buff,                       //数据接收缓冲区  
  259.         &dwInfoBufferLength,        //指示缓冲区长度(按字节)  
  260.         NULL  
  261.         );  
  262.     if(reslut){  
  263.         printf("已经成功接收%d字节报文头\n", dwInfoBufferLength);  
  264.         fwrite(Buff,sizeof(char),dwInfoBufferLength,fp);  
  265.         //puts(Buff);  
  266.         //sprintf("%s\n",Buff);  
  267.     }else{  
  268.         printf( "HttpQueryInfo failed, error = %d (0x%x)/n",  GetLastError(), GetLastError());    
  269.         return 1;  
  270.     }  
  271.   
  272.     return 0;  
  273. }  
  274.   
  275. //接收响应主体  
  276. static int Receive_Main_Part(HINTERNET hResourceHandle,FILE *fp){  
  277.     DWORD dwBytesAvailable;  
  278.     int stats;  
  279.     while(TRUE)   
  280.     {         
  281.         stats = InternetQueryDataAvailable(  
  282.             hResourceHandle,    //Handle returned by the InternetOpenUrl, FtpOpenFile, GopherOpenFile, or HttpOpenRequest function.  
  283.             &dwBytesAvailable,  //可读数据字节数量  
  284.             0,                  //使用时候必须置0  
  285.             0                   //使用时候必须置0  
  286.             );  
  287.         if(!stats)  
  288.             break;    
  289.         DWORD dwBytesRead;    
  290.         BOOL bResult = InternetReadFile(  
  291.             hResourceHandle,    //Handle returned from a previous call to InternetOpenUrl, FtpOpenFile, or HttpOpenRequest  
  292.             Buff,               //数据接收缓冲区                                        
  293.             dwBytesAvailable,   //待读取的字节数  
  294.             &dwBytesRead        //本次已经读取的字节数  
  295.             );    
  296.         if(!bResult) {    
  297.             printf("InternetReadFile failed, error = %d (0x%x)/n",  GetLastError(), GetLastError());    
  298.             break;    
  299.         }else{  
  300.             printf("已经成功接收%d字节报文主体\n", dwBytesRead);  
  301.             fwrite(Buff,sizeof(char),dwBytesRead,fp);     
  302.             puts(Buff);  
  303.         }  
  304.         if(dwBytesRead == 0)    
  305.             break;   
  306.     }    
  307.     return 0;  
  308. }  
  309.   
  310. int _tmain(int argc, _TCHAR* argv[])   
  311. {    
  312.     HINTERNET hOpenHandle,hConnectHandle,hResourceHandle;  
  313.     char str[128] = {0};  
  314.     char *s= str;  
  315.     char *command = NULL;  
  316.     //printf("请输入你要连接的网站:");  
  317.     //scanf("%s",str);  
  318.     //printf("你要连接的网站是:%s\n",str);  
  319.     //注意,在这里不要附加任何形如https:account.xiaomi.com否则,很有可能连接不上  
  320.     //s = "account.xiaomi.com";       
  321.     s = "oschina.net";    
  322.     FILE *fp ,*fp2;  
  323.     fp = fopen("报文头1.txt","wb+");  
  324.     fp2 = fopen("报文主体2.txt","wb+");  
  325.   
  326.     hOpenHandle     = Init_Internet();    
  327.   
  328.     hConnectHandle  = Set_Https_Connect_handel(s,hOpenHandle);  
  329.   
  330.   
  331.     /***********************************************************************************/  
  332.     //hResourceHandle = Set_Https_Request2(hConnectHandle,dwOpenRequestFlags);  
  333.     hResourceHandle=HttpOpenRequest(  
  334.         hConnectHandle,                     // InternetConnect handle  
  335.         L"POST",                                // Method        
  336.         L"/action/user/hash_login"  ,       // Object name        
  337.         L"HTTP/1.1",                                // Version  
  338.         L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL,                             // Referrer  
  339.         NULL,                               // Extra headers       
  340.         dwOpenRequestFlags,                 // Flags   
  341.         0                                   // Context  
  342.         );  
  343.     if(hResourceHandle == NULL)  
  344.     {  
  345.         printf("错误代码:%d",GetLastError());  
  346.         return  0;  
  347.     }  
  348.   
  349.   
  350.     LPCSTR lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";    
  351.     DWORD   dwHeadersLength=-1L;    
  352.     char szFormData[1024]={0};  
  353.     strcpy(szFormData,"email=xin&pwd=1411678a0b9e25ee2f7c8b2f7ac92b6a74b3f9c5&save_login=1");  //这里是账号密码,有些网站的登录这里的密码进行了简单的转化  
  354.     LPVOID pBuf=(LPVOID)szFormData;  
  355.     BOOL stats = HttpSendRequestA(hResourceHandle,lpszHeaders,dwHeadersLength,pBuf,strlen(szFormData));  
  356.   
  357.     if(stats)  
  358.     {  
  359.         Receive_Header(hResourceHandle,fp);  
  360.     }else{  
  361.         printf("HttpSendRequest failed, error = %d (0x%x)\n",   GetLastError(), GetLastError());    
  362.     }  
  363.     Receive_Main_Part(hResourceHandle,fp2);  
  364.   
  365.     /***********************************************************************************/  
  366.   
  367.       
  368.     hResourceHandle = HttpOpenRequest(  
  369.         hConnectHandle,                     // InternetConnect handle  
  370.         L"GET",                             // Method        
  371.         NULL,       // Object name        
  372.         L"HTTP/1.1",                                // Version  
  373.         NULL,//L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL,                              // Referrer  
  374.         NULL,                               // Extra headers       
  375.         dwOpenRequestFlags,                 // Flags  dwOpenRequestFlags  
  376.         0                                   // Context  
  377.         );  
  378.   
  379.     if(hResourceHandle==NULL)  
  380.     {  
  381.   
  382.         return 0;  
  383.     }  
  384.   // lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";    
  385.     dwHeadersLength=-1L;    
  386.     BOOL start2 = HttpSendRequest(  
  387.         hResourceHandle,    //Handle returned by HttpOpenRequest or InternetOpenUrl  
  388.         NULL,   
  389.         NULL,   
  390.         NULL,   
  391.         NULL  
  392.         );  
  393.     if(start2)  
  394.     {  
  395.         Receive_Header(hResourceHandle,fp);  
  396.     }else{  
  397.         printf("HttpSendRequest failed, error = %d (0x%x)\n",   GetLastError(), GetLastError());    
  398.     }  
  399.     Receive_Main_Part(hResourceHandle,fp2);  
  400.   
  401.       
  402.     /************************************************/  
  403.   
  404.   
  405.     Close_Internet(hResourceHandle);  
  406.     Close_Internet(hConnectHandle);  
  407.     Close_Internet(hOpenHandle);  
  408.     fclose(fp);  
  409.     fclose(fp2);  
  410.     printf("请在本工程目录下报文头.txt和报文头.txt文档查看接收的数据\n");  
  411.     system("pause");  
  412.     return 0;  
  413. }    



主题推荐
火狐浏览器 xmlhttprequest 浏览器 数据 windows
猜你在找
如何让ActiveXObject "MicrosoftXmlDom "对象在非IE浏览器下显示数据firefox火狐
如何用java实现登陆网站--不需打开浏览器
Firefox OS 20 模拟器 – 使用官方火狐浏览器扩展无痛完美模拟体验火狐手机系统
如何在PC上测试Web移动端网站和模拟手机浏览器的5大方法
Chrome浏览器模拟手机访问网站的设置方法
在PC上测试移动端网站和模拟手机浏览器的5大方法
在PC上测试移动端网站和模拟手机浏览器的5大方法
php curl函数模拟浏览器抓取网站信息
Chrome浏览器模拟手机访问网站的设置方法
Chrome浏览器模拟手机访问网站的设置方法
查看评论

  暂无评论

发表评论
  • 用 户 名:
  • ilvu999
  • 评论内容:
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
InternetOpenUrl函数 的应用
深入剖析 WinInet (转)
使用WinINet和WinHTTP实现Http访问
InternetOpen\InternetOpenUrl\InternetReadFile 等相关Win32 网络API 使用详细说明
C++ Wininet API
WinInet API 的异步方式使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服