现在事情比较多,多到不知道干什么好。抽点时间讲前一段时间学的的东西总结下,并和大家分享,主要用wininet模拟登录网站,供需要的朋友参考。
1:自己抓包分析。我是在火狐浏览器上装了个插件(Live HTTP 和Fiddler都可以),就能看到看到登录网站和登陆后的一系列操作,所有的数据都在里面。
2:就是模拟浏览器的发送请求。
Windows下用wininet比较方便,用socket也可以(如果是https的网站,还要加安全机制,我用了openssl),可以参考我前面写的代码。Wininet的一些资料请访问下面的地址。
http://download.csdn.net/detail/weiwei2012start/6922457
3:下面的我的原始代码,可能有些地方并不需要,写的有点繁琐,请大家见谅。我用的是 这个网站 oschina.net。不多说,看代码。
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <wininet.h>
#pragma comment(lib,"wininet.lib")
static char Buff[102400]; //开辟100K的缓冲区
DWORD dwOpenRequestFlags= INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_PRAGMA_NOCACHE |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_RESYNCHRONIZE |
//设置启用HTTPS
INTERNET_FLAG_SECURE |
INTERNET_FLAG_RELOAD;
/*
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI |
//设置启用HTTPS
INTERNET_FLAG_SECURE |
INTERNET_FLAG_RELOAD;*/
//字符串到unicode码转换
//入口参数:待转换字符串,返回转换后的unicode指针
TCHAR* ANSI_To_Unicode(char *str )
{
static TCHAR buffer[4096];
memset(buffer,0,sizeof(buffer));
MultiByteToWideChar(
CP_ACP,
0,
str,
-1,
buffer,
strlen(str)
);
return (TCHAR *)buffer;
}
//初始化Internet
static HINTERNET Init_Internet()
{
HINTERNET hOpenHandle;
LPCTSTR lpszAgent = L"Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0";
hOpenHandle = InternetOpen(
lpszAgent,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0
);
if(hOpenHandle == NULL)
{
printf("错误代码:%d\n",GetLastError());
}
return hOpenHandle;
}
//注销Internet句柄
static int Close_Internet(HINTERNET hInternet)
{
BOOL Stats = InternetCloseHandle( hInternet);
if(Stats == FALSE)
{
printf("错误代码:%d",GetLastError());
return 1;
}
return 0;
}
//设置internet连接句柄 适用于http
//入口参数:网址、internetOpen 句柄
static HINTERNET Set_Connect_handel(char *serverName,HINTERNET hOpenHandle){
HINTERNET hConnectHandle;
TCHAR *internetAddress = ANSI_To_Unicode(serverName);
hConnectHandle = InternetConnect(
hOpenHandle, // InternetOpen handle
internetAddress, // Server name
INTERNET_DEFAULT_HTTP_PORT, // port
NULL, // User name
NULL, // User password
INTERNET_SERVICE_HTTP, // Service
0, // Flags
0 // Context
);
if(hConnectHandle == NULL)
{
printf("错误代码:%d",GetLastError());
}
return hConnectHandle;
}
//设置internet连接句柄 适用于https
//入口参数:网址、internetOpen 句柄
static HINTERNET Set_Https_Connect_handel(char *serverName,HINTERNET hOpenHandle){
HINTERNET hConnectHandle;
TCHAR *internetAddress = ANSI_To_Unicode(serverName);
hConnectHandle = InternetConnect(
hOpenHandle, // InternetOpen handle
internetAddress, // Server name
INTERNET_DEFAULT_HTTPS_PORT, // Default HTTPS port - 443
NULL, // User name
NULL, // User password
INTERNET_SERVICE_HTTP, // Service
0, // Flags
0 // Context
);
if(hConnectHandle == NULL)
{
printf("错误代码:%d",GetLastError());
}
return hConnectHandle;
}
//设置请求报文适用于http
static HINTERNET Set_Request_Command_Http(HINTERNET hConnectHandle){
HINTERNET hResourceHandle;
hResourceHandle = HttpOpenRequest(
hConnectHandle, // InternetConnect handle
TEXT("GET"), // Method
TEXT(""), // Object name
NULL, // Version
NULL, // Referrer
NULL, // Extra headers
INTERNET_FLAG_KEEP_CONNECTION, // Flags
0 // Context
);
if(hResourceHandle == NULL)
{
printf("错误代码:%d",GetLastError());
}
return hResourceHandle;
}
static HINTERNET Set_Https_Request2(HINTERNET hConnectHandle,DWORD dwFlage){
HINTERNET hResourceHandle;
hResourceHandle = HttpOpenRequest(
hConnectHandle, // InternetConnect handle
L"POST", // Method
L"/action/user/hash_login" , // Object name
L"HTTP/1.1", // Version
L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL, // Referrer
NULL, // Extra headers
dwFlage, // Flags
0 // Context
);
if(hResourceHandle == NULL)
{
printf("错误代码:%d",GetLastError());
}
return hResourceHandle;
}
static HINTERNET Set_Https_Request3(HINTERNET hConnectHandle,DWORD dwFlage){
HINTERNET hResourceHandle;
hResourceHandle = HttpOpenRequest(
hConnectHandle, // InternetConnect handle
L"GET", // Method
NULL, // Object name
L"HTTP/1.1", // Version
NULL,//L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL, // Referrer
NULL, // Extra headers
dwFlage, // Flags
0 // Context
);
if(hResourceHandle == NULL)
{
printf("错误代码:%d",GetLastError());
}
return hResourceHandle;
}
static HINTERNET Set_Https_Request_Command2post(HINTERNET hConnectHandle){
HINTERNET hResourceHandle;
DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI |
//设置启用HTTPS
INTERNET_FLAG_SECURE |
INTERNET_FLAG_RELOAD;
hResourceHandle = HttpOpenRequest(
hConnectHandle, // InternetConnect handle
L"GET", // Method
L"/action/user/hash_login" , // Object name
L"HTTP/1.1", // Version
L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL, // Referrer
NULL, // Extra headers
dwOpenRequestFlags, // Flags
0 // Context
);
if(hResourceHandle == NULL)
{
printf("错误代码:%d",GetLastError());
}
return hResourceHandle;
}
static int Send_Request2(HINTERNET hResourceHandle){
BOOL stats;
//char postData[1000={0};
// 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
LPCSTR lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
DWORD dwHeadersLength=-1L;
char szFormData[1024]={0};
strcpy(szFormData,"email=xin&pwd=1411678a0b9e25ee2f7c8b2f7ac92b6a74b3f9c5&save_login=1");
LPVOID pBuf=(LPVOID)szFormData;
stats = HttpSendRequestA(hResourceHandle,lpszHeaders,dwHeadersLength,pBuf,strlen(szFormData));
/*
stats = HttpSendRequest(
hResourceHandle, //Handle returned by HttpOpenRequest or InternetOpenUrl
lpszHeaders,
dwHeadersLength,
pBuf,
strlen(szFormData)
); */
return stats;
}
static int Send_Request3(HINTERNET hResourceHandle){
BOOL stats;
LPCSTR lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
DWORD dwHeadersLength=-1L;
stats = HttpSendRequest(
hResourceHandle, //Handle returned by HttpOpenRequest or InternetOpenUrl
NULL,
NULL,
NULL,
NULL
);
return stats;
}
//接收报文头
static int Receive_Header(HINTERNET hResourceHandle,FILE *fp){
DWORD dwInfoBufferLength;
BOOL reslut = HttpQueryInfo(
hResourceHandle, //Handle returned by HttpOpenRequest or InternetOpenUrl
HTTP_QUERY_RAW_HEADERS_CRLF,
Buff, //数据接收缓冲区
&dwInfoBufferLength, //指示缓冲区长度(按字节)
NULL
);
if(reslut){
printf("已经成功接收%d字节报文头\n", dwInfoBufferLength);
fwrite(Buff,sizeof(char),dwInfoBufferLength,fp);
//puts(Buff);
//sprintf("%s\n",Buff);
}else{
printf( "HttpQueryInfo failed, error = %d (0x%x)/n", GetLastError(), GetLastError());
return 1;
}
return 0;
}
//接收响应主体
static int Receive_Main_Part(HINTERNET hResourceHandle,FILE *fp){
DWORD dwBytesAvailable;
int stats;
while(TRUE)
{
stats = InternetQueryDataAvailable(
hResourceHandle, //Handle returned by the InternetOpenUrl, FtpOpenFile, GopherOpenFile, or HttpOpenRequest function.
&dwBytesAvailable, //可读数据字节数量
0, //使用时候必须置0
0 //使用时候必须置0
);
if(!stats)
break;
DWORD dwBytesRead;
BOOL bResult = InternetReadFile(
hResourceHandle, //Handle returned from a previous call to InternetOpenUrl, FtpOpenFile, or HttpOpenRequest
Buff, //数据接收缓冲区
dwBytesAvailable, //待读取的字节数
&dwBytesRead //本次已经读取的字节数
);
if(!bResult) {
printf("InternetReadFile failed, error = %d (0x%x)/n", GetLastError(), GetLastError());
break;
}else{
printf("已经成功接收%d字节报文主体\n", dwBytesRead);
fwrite(Buff,sizeof(char),dwBytesRead,fp);
puts(Buff);
}
if(dwBytesRead == 0)
break;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HINTERNET hOpenHandle,hConnectHandle,hResourceHandle;
char str[128] = {0};
char *s= str;
char *command = NULL;
//printf("请输入你要连接的网站:");
//scanf("%s",str);
//printf("你要连接的网站是:%s\n",str);
//注意,在这里不要附加任何形如https:account.xiaomi.com否则,很有可能连接不上
//s = "account.xiaomi.com";
s = "oschina.net";
FILE *fp ,*fp2;
fp = fopen("报文头1.txt","wb+");
fp2 = fopen("报文主体2.txt","wb+");
hOpenHandle = Init_Internet();
hConnectHandle = Set_Https_Connect_handel(s,hOpenHandle);
/***********************************************************************************/
//hResourceHandle = Set_Https_Request2(hConnectHandle,dwOpenRequestFlags);
hResourceHandle=HttpOpenRequest(
hConnectHandle, // InternetConnect handle
L"POST", // Method
L"/action/user/hash_login" , // Object name
L"HTTP/1.1", // Version
L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL, // Referrer
NULL, // Extra headers
dwOpenRequestFlags, // Flags
0 // Context
);
if(hResourceHandle == NULL)
{
printf("错误代码:%d",GetLastError());
return 0;
}
LPCSTR lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
DWORD dwHeadersLength=-1L;
char szFormData[1024]={0};
strcpy(szFormData,"email=xin&pwd=1411678a0b9e25ee2f7c8b2f7ac92b6a74b3f9c5&save_login=1"); //这里是帐号密码,有些网站的登录这里的密码进行了简单的转化
LPVOID pBuf=(LPVOID)szFormData;
BOOL stats = HttpSendRequestA(hResourceHandle,lpszHeaders,dwHeadersLength,pBuf,strlen(szFormData));
if(stats)
{
Receive_Header(hResourceHandle,fp);
}else{
printf("HttpSendRequest failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
}
Receive_Main_Part(hResourceHandle,fp2);
/***********************************************************************************/
hResourceHandle = HttpOpenRequest(
hConnectHandle, // InternetConnect handle
L"GET", // Method
NULL, // Object name
L"HTTP/1.1", // Version
NULL,//L"https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_1160014_23246",//NULL, // Referrer
NULL, // Extra headers
dwOpenRequestFlags, // Flags dwOpenRequestFlags
0 // Context
);
if(hResourceHandle==NULL)
{
return 0;
}
// lpszHeaders = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
dwHeadersLength=-1L;
BOOL start2 = HttpSendRequest(
hResourceHandle, //Handle returned by HttpOpenRequest or InternetOpenUrl
NULL,
NULL,
NULL,
NULL
);
if(start2)
{
Receive_Header(hResourceHandle,fp);
}else{
printf("HttpSendRequest failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
}
Receive_Main_Part(hResourceHandle,fp2);
/************************************************/
Close_Internet(hResourceHandle);
Close_Internet(hConnectHandle);
Close_Internet(hOpenHandle);
fclose(fp);
fclose(fp2);
printf("请在本工程目录下报文头.txt和报文头.txt文档查看接收的数据\n");
system("pause");
return 0;
}