मैं इसे कुछ दिनों तक काम करने की कोशिश कर रहा हूं हालांकि मुझे सर्वर से 400 त्रुटि मिल रही है।बूस्ट एएसआईओ पोस्ट HTTP अनुरोध - शीर्षलेख और शरीर
असल में, मैं जो करने की कोशिश कर रहा हूं वह एक सर्वर को एक http POST अनुरोध भेजता है जिसके लिए एक JSON अनुरोध निकाय को दो गुणों के साथ आवश्यकता होती है।
इन libs मैं वर्तमान में
अपडेट --- 7/23/13 10:00 बजे बस देखा मैं HTTP यकीन नहीं कितना यह एक HTTP कॉल प्रभाव होगा लेकिन के बजाय टीसीपी उपयोग कर रहा हूँ का उपयोग कर रहा हैं मैं बूस्ट के साथ शुद्ध HTTP उपयोग करने वाले ग्राहकों के किसी भी उदाहरण नहीं मिल सकता है :: ASIO
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json;
using boost::asio::ip::tcp;
स्थापित कोड
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(part1, "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request);
JSON शरीर
+०१२३५१६४१०ptree root, info;
root.put ("some value", "8");
root.put ("message", "value value: value!");
info.put("placeholder", "value");
info.put("value", "daf!");
info.put("module", "value");
root.put_child("exception", info);
std::ostringstream buf;
write_json (buf, root, false);
std::string json = buf.str();
शीर्षक और कनेक्शन अनुरोध
request_stream << "POST /title/ HTTP/1.1 \r\n";
request_stream << "Host:" << some_host << "\r\n";
request_stream << "User-Agent: C/1.0";
request_stream << "Content-Type: application/json; charset=utf-8 \r\n";
request_stream << json << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
// Send the request.
boost::asio::write(socket, request);
मैं तथापि स्थान धारक मूल्यों डाल आप कुछ भी है कि मेरे कोड है कि बाहर कूदता में काम नहीं करता यदि मुझे पता है मुझे पता नहीं क्यों मैं एक मिलती रहती है तो कृपया 400 गलत अनुरोध। रिग
सी ++
WIN7
दृश्य स्टूडियो
पूरा अनुरोध भेजने से पहले, कुछ लॉग या कंसोल में प्रिंट करें और देखें कि यह सही है या नहीं। –
क्या आप हेडर फ़ील्ड के बीच 'json' लिख रहे हैं? – PSIAlt
यह वह जगह है जहां जेसन फ़ाइल को 'request_stream << json << "\ r \ n" इनपुट किया जा रहा है;' मैं अनुरोध बफर के साथ डीबगर के माध्यम से request_stream बफर को देख रहा हूं। सामग्री के संदर्भ में सबकुछ सही लगता है, हालांकि कुछ अजीब रिक्त स्थान हैं जो आपको लगता है कि इसका असर हो सकता है? – progrenhard