问题描述
- 关于HttpSendRequestA函数发送请求成功后,保存网页返回内容的问题
-
下边的代码是点击按钮事件的,HttpSendRequest明显已经执行成功,得到的recv.html中依然是登陆页面,不是我想要的登陆后跳过去的页面#include <afxinet.h> #include <WinInet.h> #include <cstring> #pragma comment(lib,"wininet.lib") void CLOGINDlg::OnOK() { // TODO: Add extra validation here //CDialog::OnOK(); HINTERNET hSession = NULL; HINTERNET hConnect = NULL; HINTERNET hRequest = NULL; //InternerOpen LPCSTR lpszAgent = "Application"; DWORD dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG; /* HINTERNET InternetOpen( _In_ LPCTSTR lpszAgent, //指定调用Wininet函数的应用程序或入口,该入口作HTTP协议中用户代理项 _In_ DWORD dwAccessType, //访问要求类型 _In_ LPCTSTR lpszProxyName, //代理服务器的名字 _In_ LPCTSTR lpszProxyBypass,//指向一个字符串,指定一个可选的主机名列表或IP地址 _In_ DWORD dwFlags ); */ hSession = InternetOpenA(lpszAgent, dwAccessType, NULL, NULL, 0); if (!hSession) { printf("网络打开失败!"); return ; } //InternetConnectA LPCSTR lpszServerName = "202.200.144.63"; INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTP_PORT; DWORD dwService = INTERNET_SERVICE_HTTP; hConnect = InternetConnectA(hSession, lpszServerName, nServerPort, NULL, NULL, dwService, 0, 1); if (!hConnect) { printf("网络连接失败!"); return ; } //HttpOpenRequestA LPCSTR lpszVerb = "GET"; LPCSTR lpszObjectName = "xs_main.aspx?xh=120620223";//网站的对象名,网址在IntetnetConnectA第二个参数指定 LPCSTR lpszVersion = "HTTP/1.1"; LPCSTR lpszReferer = "Referer:http://202.200.144.63/xs_main.aspx?xh=120620223 "; LPCSTR lpszAcceptTypes = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; DWORD dwFlags = INTERNET_FLAG_RELOAD; hRequest = HttpOpenRequestA(hConnect, lpszVerb, lpszObjectName, lpszVersion, lpszReferer, &lpszAcceptTypes, dwFlags, 1); if (!hRequest) { printf("打开请求失败!"); return ; } //HttpSendRequestA LPCSTR lpszHeaders = "Content-Type: application/x-www-form-urlencoded;Cookie: safedog-flow-item=F727C5A572FD29201828136C14E22568; ASP.NET_SessionId=uxizon55w1ua4djitykxu3vlrn"; DWORD dwHeadersLength = -1L; char szFormData[1024] = { 0 }; strcpy(szFormData, "__VIEWSTATE=dDwyODE2NTM0OTg7Oz4h2wC4w12c8RCRF3jDvJBjVLPUHQ%3D%3D&txtUserName=120620223&TextBox2=******&txtSecretCode="+m_YZM+"&RadioButtonList1=%D1%A7%C9%FA&Button1=&lbLanguage=&hidPdrs=&hidsc=rn");//保存用户名,密码,用户类别及验证码 LPVOID pBuf = (LPVOID)szFormData; BOOL bReturn = HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, NULL, 0); if (!bReturn) { printf("返回请求失败!"); return ; }else AfxMessageBox("登陆成功!"); char szRecvBuf[1024 + 1]; // 接受数据缓冲区 DWORD dwNumberOfBytesRead; // 服务器返回大小 DWORD dwRecvTotalSize = 0; // 接受数据总大小 DWORD dwRecvBuffSize = 0; // 接受数据buf的大小 memset(szRecvBuf, 0, 1024 + 1); LPCSTR lpFileName = "recv.html"; DWORD dwNumberOfBytesWritten; HANDLE hOpenFile = (HANDLE)CreateFileA(lpFileName, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, NULL, NULL); if (hOpenFile == INVALID_HANDLE_VALUE) { printf("打开文件失败!"); hOpenFile = NULL; return ; } do { bReturn = InternetReadFile(hRequest, szRecvBuf, 1024, &dwNumberOfBytesRead); if (!bReturn) { printf("读取文件失败!"); break; } // 统计接受数据的大小 szRecvBuf[dwNumberOfBytesRead] = ''; dwRecvTotalSize += dwNumberOfBytesRead; dwRecvBuffSize += strlen(szRecvBuf); WriteFile(hOpenFile, szRecvBuf, dwNumberOfBytesRead, &dwNumberOfBytesWritten, NULL); } while (dwNumberOfBytesRead != 0); CloseHandle(hOpenFile); InternetCloseHandle(hSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); }
解决方案
加不加验证码的结果一模一样,验证码好像没起多大作用。我是只加把显示验证码那个链接用CHtmlView的 Navigate方法显示在我自己的登陆界面了,但是输入不输入结果都一样,都是“登陆成功”,但是我得到的recv.html文件依旧是网页里登陆的那个界面,没发生变化
解决方案二:
你用api方式提交的数据跟浏览器有区别,你可以抓包对比一下数据,看哪里得数据参数没有填对。
时间: 2024-10-04 12:08:06