用wininet库模拟cookie方式验证码登录
#include "stdafx.h" #include <windows.h> #include <wininet.h> #include <tchar.h> #include <conio.h> #include <time.h> DWORD WriteDataToFile(LPSTR lpFileName, LPSTR data, int size) { DWORD dwWritten; HANDLE hFile = CreateFileA(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if (hFile == INVALID_HANDLE_VALUE) return 0; if (!WriteFile(hFile, (LPCVOID)data, size, &dwWritten, NULL)) return 0; CloseHandle(hFile); return dwWritten; } int main(int argc, char* argv[]) { static TCHAR hdrs[] = _T("Accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, */*\r\n" "Accept-Language: zh-cn\r\n" "Accept-Encoding: gzip, deflate\r\n" "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; TheWorld)\r\n" ); static TCHAR phdrs[] = _T("Accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, */*\r\n" "Accept-Language: zh-cn\r\n" "Accept-Encoding: gzip, deflate\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Connection: Keep-Alive\r\n" "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; TheWorld)\r\n" "Content-Length: 59\r\n" ); static TCHAR frmdata[] = _T("wen=368616592221&code=%s&Submit2=%C9%EA%CD%A8%B2%E9%D1%AF"); // Submit2=%C9%EA%CD%A8%B2%E9%D1%AF 申通查询 char pdata[MAX_PATH] = ""; const TCHAR *accept = _T("Accept: */*"); HINTERNET hSession = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); HINTERNET hConnect = InternetConnect(hSession, _T("my.kiees.cn"), 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); HINTERNET hRequest = HttpOpenRequest(hConnect, "GET", _T("/stocode.php"), NULL, NULL, &accept, 0, 1); HttpSendRequest(hRequest, hdrs, strlen(hdrs), 0, 0); // 这里没有cookie //HttpSendRequest(hRequest, NULL, 0, 0, 0); DWORD dwBytesRead; char buffer[MAX_PATH * 50] = ""; ::InternetReadFile(hRequest, buffer, MAX_PATH * 50, &dwBytesRead); // 下载图片, 保存到文件 WriteDataToFile("AA.JPG", buffer, dwBytesRead); //WinExec("AA.JPG", SW_SHOW); printf("Input code...\n"); char code[MAX_PATH]; gets(code); wsprintf(pdata, "wen=368019074201&code=%s&Submit2=%%C9%%EA%%CD%%A8%%B2%%E9%%D1%%AF", code); HINTERNET hPostRequest = HttpOpenRequest(hConnect, "POST", _T("/sto.php"), NULL, NULL, &accept, 0, 1); HttpSendRequest(hPostRequest, phdrs, strlen(phdrs), pdata, strlen(pdata)); // 这里抓包带了cookie ::InternetReadFile(hPostRequest, buffer, MAX_PATH * 50, &dwBytesRead); WriteDataToFile("AA.html", buffer, dwBytesRead); return 0; }