91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

網絡通信第四課 C++發送Post請求的完整案例

發布時間:2020-06-13 14:51:06 來源:網絡 閱讀:2094 作者:fengyuzaitu 欄目:系統運維

說明

當前例子采用boost asio庫發送HTTP數據報文,采用boost::asio::streambuf request流封裝數據報文,在例子的后面

通過string對響應數據包進行分析,獲取其中的json格式數據包


#include <iostream>

#include <fstream>

#include <string>

#include <boost/asio.hpp>


using namespace std;

using namespace boost::asio;



unsigned char ToHex(unsigned char x)

{

return  x > 9 ? x + 55 : x + 48;

}


unsigned char FromHex(unsigned char x)

{

unsigned char y;

if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;

else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;

else if (x >= '0' && x <= '9') y = x - '0';

else assert(0);

return y;

}


std::string UrlEncode(const std::string& str)

{

std::string strTemp = "";

size_t length = str.length();

for (size_t i = 0; i < length; i++)

{

if (isalnum((unsigned char)str[i]) ||

(str[i] == '-') ||

(str[i] == '_') ||

(str[i] == '.') ||

(str[i] == '~') ||

(str[i] == '&') ||

(str[i] == '='))

strTemp += str[i];

else if (str[i] == ' ')

strTemp += "+";

else

{

strTemp += '%';

strTemp += ToHex((unsigned char)str[i] >> 4);

strTemp += ToHex((unsigned char)str[i] % 16);

}

}

return strTemp;

}


std::string UrlDecode(const std::string& str)

{

std::string strTemp = "";

size_t length = str.length();

for (size_t i = 0; i < length; i++)

{

if (str[i] == '+') strTemp += ' ';

else if (str[i] == '%')

{

assert(i + 2 < length);

unsigned char high = FromHex((unsigned char)str[++i]);

unsigned char low = FromHex((unsigned char)str[++i]);

strTemp += high * 16 + low;

}

else strTemp += str[i];

}

return strTemp;

}




int HttpPost()

{

// char szSrcBuffer[1024] = { "authorJson={loginAccount:\"admin\"}&parmJson={code:\"SheBeiLiXianGaoJingShangChuan\",params:{id:\"123456\",remarks:\"fire alarm\",time_alarm:\"2017-1-1\",alarm_source:\"192.168.1.11\"}}" };

char szSrcBuffer[1024] = { "authorJson={loginAccount:\"admin\"}&parmJson={code:\"SheBeiLiXianChaXun\",params:{}}" };

std::string strUrlEnCodedBuffer = UrlEncode(szSrcBuffer);


io_service iosev;

ip::tcp::socket socket(iosev);

ip::tcp::endpoint ep(ip::address_v4::from_string("192.168.0.88"), 8080);

boost::system::error_code ec;

socket.connect(ep, ec);

if (ec) return -1;


boost::asio::streambuf request;

std::ostream request_stream(&request);


request_stream << "POST /cs/restfull/operationRestfullApi/excuteSqlByCode HTTP/1.1\r\n";

request_stream << "Host: 192.168.0.88:8080\r\n";

//request_stream << "Connection: keep-alive\r\n";

request_stream << "Content-Length: " << strUrlEnCodedBuffer.size() << "\r\n";

request_stream << "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";

//request_stream << "User-Agent: Mozilla/4.0\r\n";

//request_stream << "Accept-Language: zh-CN\r\n\r\n";

request_stream << "\r\n";

request_stream << strUrlEnCodedBuffer;


size_t len = boost::asio::write(socket, request);


char szRecvBuf[1024] = { 0 };

socket.read_some(buffer(szRecvBuf), ec);

std::cout << "Http Response Context:" << std::endl;

std::cout << szRecvBuf << std::endl;


std::string strRecvBuf = szRecvBuf;

int nIndexOfBody = strRecvBuf.find("\r\n\r\n", 0);

if (nIndexOfBody < 0) return -1;


std::string strHttpBody = strRecvBuf.substr(nIndexOfBody + 4);

std::cout << "Http Body Context:" << std::endl;

std::cout << strHttpBody << std::endl;


return 0;

}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

鄂托克前旗| 宁化县| 林州市| 仲巴县| 东阳市| 西平县| 海南省| 浦县| 嘉祥县| 徐州市| 江都市| 南京市| 康保县| 乡城县| 五常市| 兰州市| 丹巴县| 太仆寺旗| 康马县| 凉城县| 理塘县| 台南市| 静安区| 红河县| 昌平区| 伊金霍洛旗| 都匀市| 阆中市| 英德市| 天全县| 长沙市| 荆州市| 民权县| 金坛市| 东乌珠穆沁旗| 项城市| 萍乡市| 吴桥县| 泸定县| 霍山县| 昌都县|