Wininet-Post

#include "stdafx.h"
#include <Windows.h>
#include <wininet.h>
#include <iostream>
#include <fstream>
#include <time.h>
//#include <MAPIUTIL.H >

using namespace std;
#pragma comment(lib, "wininet.lib")

#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif

//简单日志
void WriteLog(const char *p_filename, const char *p_str)
{
tm *local_time;
time_t t_tm = time(NULL);
local_time = localtime(&t_tm);
char chtm[20] = {0};
strftime(chtm, 20, "%H:%M:%S", local_time);
cout<<chtm<<"\t"<<p_str<<endl;
ofstream out_file(p_filename, ios::out|ios::app);
out_file<<chtm<<"\t"<<p_str<<endl;
out_file.close();
}


/* 生成GUID码 */
const char* newGUID()
{
//srand(time(NULL));
static char buf[64] = {0};
_snprintf(buf, sizeof(buf) ,
"%08X-%04X-%04X-%04X-%04X%04X%04X" ,
rand()&0xffffffff,
rand()&0xffff,
rand()&0xffff,
rand()&0xffff,
rand()&0xffff, rand()&0xffff, rand()&0xffff
);
return (const char*)buf;
}

/************************************************************************/
/* HttpPost_Data - HTTP投递数据
参数:pszURL - 投递目标地址
pszData - 投递内容
pCookie - 连接Cookie
dwCookie - Cookie长度
返回值;BOOL
*/
/************************************************************************/
BOOL HttpPost_Data(__in const TCHAR *pszURL, __in const TCHAR *pszData, __inout TCHAR *pCookie, __inout DWORD& dwCookie)
{
BOOL bRet = FALSE;
DWORD dwErr = 0;
HINTERNET hInt,hConn,hReq;

//解析地址
URL_COMPONENTS crackedURL;
TCHAR szHostName[128] = {0};
TCHAR szUrlPath[256] = {0};

ZeroMemory(&crackedURL, sizeof (URL_COMPONENTS));
crackedURL.dwStructSize = sizeof (URL_COMPONENTS);
crackedURL.lpszHostName = szHostName;
crackedURL.dwHostNameLength = sizeof(szHostName);
crackedURL.lpszUrlPath = szUrlPath;
crackedURL.dwUrlPathLength = sizeof(szUrlPath);

InternetCrackUrl(pszURL, _tcslen(pszURL),0,&crackedURL);

//启用HTTP协议
hInt = InternetOpen(_T("Microsoft Internet Explorer"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
//建立HTTP连接
hConn = InternetConnect(hInt,crackedURL.lpszHostName, crackedURL.nPort,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
//创建一个URL请求
hReq = ::HttpOpenRequest(hConn, _T("POST"), crackedURL.lpszUrlPath, NULL, NULL, NULL, INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD, 0);
dwErr = ::GetLastError();//返回0

//添加请求头部数据
const TCHAR *psz_Content_Type = _T("Content-Type: application/json");
DWORD dwLen_psz_Content_Type = _tcslen(psz_Content_Type);
HttpAddRequestHeaders(hReq, psz_Content_Type, dwLen_psz_Content_Type, HTTP_ADDREQ_FLAG_ADD);

//添加Cookie
tstring str = _T("Cookie: ");
tstring strCookie = pCookie;
str += strCookie;
HttpAddRequestHeaders(hReq, str.c_str(), str.length(), HTTP_ADDREQ_FLAG_ADD);

//发送请求
DWORD dwLen = _tcslen(pszData);
bRet = ::HttpSendRequest(hReq, NULL, 0, (LPVOID)pszData, dwLen);
if (!bRet)
{
dwErr = ::GetLastError();
//写日志...
return FALSE;
}

//查询状态码
TCHAR szCode[6] = {0};
DWORD dwCode = 6;
bRet = ::HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE , (LPVOID)szCode, &dwCode, NULL);
if (!bRet)
{
dwErr = ::GetLastError();
//写日志...szCode
return FALSE;
}

//获取Cookie数据
bRet = ::HttpQueryInfo(hReq, HTTP_QUERY_SET_COOKIE , pCookie, &dwCookie, NULL);
if (!bRet)
{
dwErr = ::GetLastError();
//写日志...szCode
}

//读取接收内容
TCHAR chRead[1024] = {0};
DWORD dwRead = 1024;
DWORD dwOutSize = 0;

tstring strRead = _T("");
while(1)
{
bRet = InternetReadFile(hReq, (LPVOID)chRead, dwRead, &dwOutSize);
if (bRet)
{
//保存解析接收到的内容
str += chRead;
}
else
{
dwErr = ::GetLastError();
break;
}
ZeroMemory(chRead, 1024);
}
bRet = TRUE;
return bRet;
}

 

int _tmain(int argc, _TCHAR* argv[])
{
/*srand(time(NULL));
for (int i = 0; i < 100; i++)
{
printf("%s\n", newGUID());
}*/

TCHAR ch[1024] ={0};
DWORD dw = 1024;
const TCHAR *pcsUrlLogin = _T("http://www.baidu.com");
const TCHAR *pszch = _T("ABCD");
DWORD dwLen = _tcslen(pszch);
HttpPost_Data(pcsUrlLogin, pszch, ch, dw);
system("pause");
return 0;
}

你可能感兴趣的:(post)