c++工程中发送http请求的示例
2016-01-28 15:22
645 查看
#include <windows.h> #include <iostream> #include <fstream> #include <string> #include "Wininet.h" #pragma comment(lib,"Wininet.lib") extern std::string g_remotIp = "192.168.1.100"; extern int g_remotPort = 8080; extern std::string g_requestString = "/cfg/test.html?message:"; extern std::string g_requestMethod = "POST"; bool sendHttpRequest(const std::string &sEvent) { if ( g_remotIp.empty() || (g_remotPort == 0) || g_requestString.empty() || g_requestMethod.empty() ) { std::cout << " sendHttpRequest failed! paremeter is invalied! " << std::endl; return false; } HINTERNET hInternet, hConnect, hRequest; BOOL bRet; std::string strResponse; WCHAR wCharStrTemp[256]; char2wchar("User-Agent", wCharStrTemp, 256); hInternet = (HINSTANCE)InternetOpen(wCharStrTemp, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (!hInternet) goto Ret0; WCHAR wCharStrRemotIp[256]; char2wchar(const_cast<char*>(g_remotIp.c_str()), wCharStrRemotIp, 256); char2wchar("HTTP/1.1", wCharStrTemp, 256); hConnect = (HINSTANCE)InternetConnect(hInternet, wCharStrRemotIp, g_remotPort, NULL, wCharStrTemp, INTERNET_SERVICE_HTTP, 0, 0); if (!hConnect) goto Ret0; WCHAR wCharMethod[256]; WCHAR wCharRequestMsg[256]; g_requestString += sEvent; char2wchar(const_cast<char*>(g_requestMethod.c_str()), wCharMethod, 256); char2wchar(const_cast<char*>(g_requestString.c_str()), wCharRequestMsg, 256); hRequest = (HINSTANCE)HttpOpenRequest(hConnect, wCharMethod, wCharRequestMsg, wCharStrTemp, NULL, NULL, INTERNET_FLAG_RELOAD, 0); if (!hRequest) goto Ret0; ////bRet = HttpAddRequestHeaders(hRequest,"Content-Type: application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD); ////if(!bRet) ////goto Ret0; bRet = HttpSendRequest(hRequest, NULL, 0, NULL, 0); unsigned long recvLen = 0; while (TRUE) { char cReadBuffer[1024 * 10] = { 0 }; unsigned long lNumberOfBytesRead; bRet = InternetReadFile(hRequest, cReadBuffer, sizeof(cReadBuffer) - 1, &lNumberOfBytesRead); if (!bRet || !lNumberOfBytesRead) break; cReadBuffer[lNumberOfBytesRead] = 0; strResponse = strResponse + cReadBuffer; recvLen += lNumberOfBytesRead; } Ret0: if (hRequest) InternetCloseHandle(hRequest); if (hConnect) InternetCloseHandle(hConnect); if (hInternet) InternetCloseHandle(hInternet); std::cout << "Recved httpServer msg: " << strResponse << " , recved len: " << recvLen << std::endl; return true; } int main() { std::string sendMsg = "hello world"; // send msg is: 192.168.1.100/cfg/test.html?message:hello world sendHttpRequest( sendMsg ); }
相关文章推荐
- Android之网络编程(四)解析 JSON 格式数据
- 使用中国CDN网络分发Unity网页插件的方法
- UDP通信
- 【java网络编程】之四
- 【java网络编程】之三
- 【java网络编程】之二
- android网络编程--HttpURLConnection(结合Handler和子线程)
- HTTPURLConnection参数详解
- 【java网络编程】之一
- Android之网络编程(三)使用 HttpClient
- XMLHttpRequest2 异步 ajax
- Ajax comet XMLHttpRequest 异步
- 单元测试中模拟HttpContext.Current
- Android之网络编程(二)使用 HttpURLConnection
- Let’s encrypt 计划为网站提供免费的基本 SSL 证书,以加速互联网从 HTTP 向 HTTPS 过渡。
- Android之网络编程(一)WebView 的用法
- 网络数据包收发流程(四):协议栈之packet_type
- 进行网络出现问题Error Domain=kCFErrorDomainCFNetwork Code=-1022 "The resource could not be loaded because th
- 通信协议:HTTP、TCP、UDP
- 网络数据包收发流程(三):e1000网卡和DMA