// HttpRequest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "HttpRequest.h"
#include <afxwin.h>
#include <stdio.h>
#include <windows.h>
#include "Wininet.h"
#pragma comment(lib,"Wininet.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
//模拟浏览器发送HTTP请求函数
CString HttpRequest(char * lpHostName,short sPort,char * lpUrl,char * lpMethod,char * lpPostData,int nPostDataLen)
{
HINTERNET hInternet,hConnect,hRequest;
BOOL bRet;CString strResponse;
hInternet = InternetOpenA("User-Agent",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if(!hInternet)
goto Ret0;
hConnect = InternetConnectA(hInternet,lpHostName,sPort,NULL,"HTTP/1.1",INTERNET_SERVICE_HTTP,0,0);
if(!hConnect)
goto Ret0;
hRequest = HttpOpenRequestA(hConnect,lpMethod,lpUrl,"HTTP/1.1",NULL,NULL,INTERNET_FLAG_RELOAD,0);
if(!hRequest)
goto Ret0;
//bRet = HttpAddRequestHeadersA(hRequest,"Content-Type: text/plain;charset=UTF-8",strlen("Content-Type: text/plain;charset=UTF-8"),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
//if(!bRet)
//goto Ret0;
bRet = HttpSendRequestA(hRequest,NULL,0,lpPostData,nPostDataLen);
//while (TRUE)
//{
// char cReadBuffer[1024] = {0};
// DWORD dwSize = 1024;
// bRet = ::HttpQueryInfoA(hRequest, HTTP_QUERY_CONTENT_BASE, cReadBuffer, &dwSize, 0);
// strResponse = cReadBuffer;
//}
while(TRUE)
{
char cReadBuffer[4096];
unsigned long lNumberOfBytesRead;
bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
if(!bRet || !lNumberOfBytesRead)
break;
cReadBuffer[lNumberOfBytesRead] = 0;
strResponse = strResponse + cReadBuffer;
}
Ret0:
if(hRequest)
InternetCloseHandle(hRequest);
if(hConnect)
InternetCloseHandle(hConnect);
if(hInternet)
InternetCloseHandle(hInternet);
return strResponse;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
//CString strResponse = HttpRequest("translate.google.com",80,"/translate_t?langpair=en|zh-CN","POST","hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN",strlen("hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN"));
CHAR bufData[MAX_PATH] = {0};
sprintf_s(bufData, MAX_PATH, "VER=1.1&CMD=Login&SEQ=1234567&UIN=474557378&PS=6376c32aa4664c4a4404099e73ad5e7a&M5=1&LC=9326B87B234E7235");
CString strResponse = HttpRequest("tqq.tencent.com",8000,NULL,"POST",bufData,strlen(bufData));
//CString strResponse = HttpRequest("www.hao123.com",80,NULL, "GET", NULL, 0);
FILE * fp = fopen("D:\\123.htm","wb");
fwrite(strResponse,strResponse.GetLength(),1,fp);
fclose(fp);
::MessageBoxA(NULL,strResponse,"123",0);
}
return nRetCode;
}
//
#include "stdafx.h"
#include "HttpRequest.h"
#include <afxwin.h>
#include <stdio.h>
#include <windows.h>
#include "Wininet.h"
#pragma comment(lib,"Wininet.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
//模拟浏览器发送HTTP请求函数
CString HttpRequest(char * lpHostName,short sPort,char * lpUrl,char * lpMethod,char * lpPostData,int nPostDataLen)
{
HINTERNET hInternet,hConnect,hRequest;
BOOL bRet;CString strResponse;
hInternet = InternetOpenA("User-Agent",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if(!hInternet)
goto Ret0;
hConnect = InternetConnectA(hInternet,lpHostName,sPort,NULL,"HTTP/1.1",INTERNET_SERVICE_HTTP,0,0);
if(!hConnect)
goto Ret0;
hRequest = HttpOpenRequestA(hConnect,lpMethod,lpUrl,"HTTP/1.1",NULL,NULL,INTERNET_FLAG_RELOAD,0);
if(!hRequest)
goto Ret0;
//bRet = HttpAddRequestHeadersA(hRequest,"Content-Type: text/plain;charset=UTF-8",strlen("Content-Type: text/plain;charset=UTF-8"),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
//if(!bRet)
//goto Ret0;
bRet = HttpSendRequestA(hRequest,NULL,0,lpPostData,nPostDataLen);
//while (TRUE)
//{
// char cReadBuffer[1024] = {0};
// DWORD dwSize = 1024;
// bRet = ::HttpQueryInfoA(hRequest, HTTP_QUERY_CONTENT_BASE, cReadBuffer, &dwSize, 0);
// strResponse = cReadBuffer;
//}
while(TRUE)
{
char cReadBuffer[4096];
unsigned long lNumberOfBytesRead;
bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
if(!bRet || !lNumberOfBytesRead)
break;
cReadBuffer[lNumberOfBytesRead] = 0;
strResponse = strResponse + cReadBuffer;
}
Ret0:
if(hRequest)
InternetCloseHandle(hRequest);
if(hConnect)
InternetCloseHandle(hConnect);
if(hInternet)
InternetCloseHandle(hInternet);
return strResponse;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
//CString strResponse = HttpRequest("translate.google.com",80,"/translate_t?langpair=en|zh-CN","POST","hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN",strlen("hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN"));
CHAR bufData[MAX_PATH] = {0};
sprintf_s(bufData, MAX_PATH, "VER=1.1&CMD=Login&SEQ=1234567&UIN=474557378&PS=6376c32aa4664c4a4404099e73ad5e7a&M5=1&LC=9326B87B234E7235");
CString strResponse = HttpRequest("tqq.tencent.com",8000,NULL,"POST",bufData,strlen(bufData));
//CString strResponse = HttpRequest("www.hao123.com",80,NULL, "GET", NULL, 0);
FILE * fp = fopen("D:\\123.htm","wb");
fwrite(strResponse,strResponse.GetLength(),1,fp);
fclose(fp);
::MessageBoxA(NULL,strResponse,"123",0);
}
return nRetCode;
}