用wininet库模拟cookie方式验证码登录(C++)

#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;
}
复制代码
namespace DatabaseCon { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static string strCon = ""; private void Form1_Load(object sender, EventArgs e) { textBox6.Text = "(local)"; } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "*.mdb(Access数据库文件)|*.mdb|*.xls(Excel文件)|*.xls|*.*(所有文件)|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void button2_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.ShowDialog(); textBox6.Text = Form2.strServer; } private void button3_Click(object sender, EventArgs e) { if (radioButton1.Checked == true) { if (textBox1.Text != "") { FileInfo FInfo = new FileInfo(textBox1.Text); string strExtention = FInfo.Extension; if (strExtention.ToLower() == ".mdb") { if (textBox2.Text != "") { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";UID=" + textBox2.Text + ";PWD=" + textBox3.Text + ";"; } else { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";"; } } else if (strExtention.ToLower() == ".xls") { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties=Excel 8.0;"; } } OleDbConnection oledbcon = new OleDbConnection(strCon); try { oledbcon.Open(); richTextBox1.Clear(); richTextBox1.Text = strCon + "\n连接成功……"; } catch { richTextBox1.Text = "连接失败"; } } else if (radioButton2.Checked == true) { if (checkBox1.Checked == true) { strCon = "Data Source=" + textBox6.Text + ";Initial Catalog =" + comboBox1.Text + ";Integrated Security=SSPI;"; } else if (checkBox2.Checked == true) { strCon = "Data Source=" + textBox6.Text + ";Database=" + comboBox1.Text + ";Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";"; } SqlConnection sqlcon = new SqlConnection(strCon); try { sqlcon.Open(); richTextBox1.Clear(); richTextBox1.Text = strCon + "\n连接成功……"; } catch { richTextBox1.Text = "连接失败"; } } }
原文地址:https://www.cnblogs.com/german/p/5933640.html