Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Network Communication lesson 4 A complete case of a Post request sent by C++

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Description

The current example uses boost asio library to send HTTP data message, and uses boost::asio::streambuf request stream to encapsulate the data message, at the end of the example.

The response packet is analyzed by string to obtain the json format packet.

# include

# include

# include

# include

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 ='a'& & x ='0' & & x > 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; iTunes +)

{

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report