WinInte API 发送 HTTP 请求

BOOL bResult = FALSE;<?XML:NAMESPACE PREFIX = O />

// 初始化WinInet 环境

HINTERNET hInternet = InternetOpen("CEHTTP", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);

// 打开http session

HINTERNET hSession = InternetConnect(hInternet, "www.myserver.com.cn", 8080, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

// 打开http post 请求的句柄

char* szAccept[] = {"*/*", NULL};

HINTERNET hRequest = HttpOpenRequest(hSession, "POST", "/myfolder/myfile.jsp", NULL, NULL, (LPCSTR*)szAccept, INTERNET_FLAG_NO_CACHE_WRITE, 0);

// 准备发送的xml 数据,可以是任意长度

char post_data[] = "<Root>...</Root>";

// 外发的header

char headerLanguage[] = "Accept-Language: zh-cn/r/n";

char headerEncoding[] = "Accept-Encoding: gzip, deflate/r/n";

char headerContentType[] = "Content-Type: text/xml/r/n";

char headerContentLength[64];

sprintf(headerContentLength, "Content-Length: %d/r/n", strlen(post_data));

// 添加header 信息

bResult = HttpAddRequestHeaders(hRequest, headerLanguage, -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

bResult = HttpAddRequestHeaders(hRequest, headerEncoding, -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

bResult = HttpAddRequestHeaders(hRequest, headerContentType, -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

bResult = HttpAddRequestHeaders(hRequest, headerContentLength, -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

/*=====================================================================

// 简单的发送数据的方法,可用来发送少量数据,或提交GET请求

//===================================================================*/

// bResult = HttpSendRequest(hRequest, NULL, 0, post_data, );

/*=====================================================================

// 发送大量数据包的方法

//===================================================================*/

INTERNET_BUFFERS BufferIn = {0};

BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );

BufferIn.dwBufferTotal = strlen(post_data);

bResult = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0, 0);

DWORD written = 0;

bResult = InternetWriteFile(hRequest, (LPVOID)(LPCTSTR)post_data, strlen(post_data), &written);

bResult = HttpEndRequest(hRequest, NULL, 0, 0);

/*=====================================================================

// 发送大量数据包结束

//===================================================================*/

LPSTR lpszData = NULL; // buffer for the data

DWORD dwSize = 0; // size of the data available

DWORD dwDownloaded = 0; // size of the downloaded data

// 请求header 的大小,注意这里的bResult FALSE

bResult = HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize, NULL);

// 为接收header 分配内存空间

CHAR* lpHeadersA = new CHAR [dwSize];

// 接收http response 中的header

bResult = HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID) lpHeadersA, &dwSize, NULL);

// 循环读取数据

while(1)

{

// 检查在http response 还有多少字节可以读取

if (!InternetQueryDataAvailable(hRequest,&dwSize,0,0))

{

break;

}

else

{

// 分配内存

lpszData = new char[dwSize+1];

// 读取数据

if(!InternetReadFile(hRequest,(LPVOID)lpszData,dwSize,&dwDownloaded))

{

delete[] lpszData;

break;

}

else

{

// 处理的得到的数据...

// 检查还有没有剩余数据

if (dwDownloaded == 0)

break;

}

}

}

// 关闭句柄

InternetCloseHandle(hRequest);

InternetCloseHandle(hSession);

InternetCloseHandle(hInternet);

// 注意:在使用HttpSendRequestEx 后,必须使用HttpEndRequest

// 才能开始请求header 信息,接收数据。

你可能感兴趣的:(http)