我已成功使用 AFHTTPRequestOperationManager 将JSON数据发送到 BOOST Asio ,但检索到JSON数据失败。我确保可以通过 AFNetworking 中的可达性功能与主机联系。我已经尝试了几个基于AFHTTPRequestOperationManager的函数,例如:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates=YES;
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://xx.xx.xx.x:8888"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
但是,我一直得到同样的错误:
错误域= NSURLErrorDomain代码= -1005"网络连接是 丢失&#34。 UserInfo = {NSUnderlyingError = 0x12fd7c580 {错误 Domain = kCFErrorDomainCFNetwork Code = -1005"网络连接是 丢失&#34。的UserInfo = {NSErrorFailingURLStringKey = HTTP:/xx.xx.xx.x:8888 /, _kCFStreamErrorDomainKey = 1,NSErrorPeerAddressKey = {length = 16,capacity = 16,bytes = 0x100222b82d3741040000000000000000},_ kCFStreamErrorCodeKey = 54, NSErrorFailingURLKey = http://xx.xx.xx.x:8888/, NSLocalizedDescription =网络连接丢失。}}, NSErrorFailingURLStringKey = http://xx.xx.xx.x:8888/, NSErrorFailingURLKey = http://xx.xx.xx.x:8888/, _kCFStreamErrorDomainKey = 1,_kCFStreamErrorCodeKey = 54,NSLocalizedDescription =网络连接丢失。}
Linux服务器示例使用 Boost Asio async_write 功能和写入处理程序来确保完成JSON POST。
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
using boost::property_tree::ptree;
using boost::property_tree::basic_ptree;
using boost::asio::ip::tcp;
using namespace std;
typedef std::string Blob;
const std::string NOOBJECT = "a";
class Server : public boost::enable_shared_from_this<Server> {
private:
const std::string AUTHSVCS = "xx.xx.xx.x:8888";
public:
Server();
virtual ~Server();
void send(tcp::socket&, boost::asio::io_service&, std::string&);
void on_send(const boost::system::error_code &, size_t);
void genheader(Blob& body, std::string& request);
void genbody(const std::string& reqtype, Blob& token, Blob& body);
};
Server::Server() {
}
Server::~Server() {
}
void
Server::genheader(Blob& body, std::string& request)
{
cout << "Server::genheader" << endl;
request += "POST / HTTP/1.1 \r\n";
request += "Host:";
request += AUTHSVCS;
request += "\r\n";
request += "User-Agent: ProteAuth/1.0 \r\n";
request += "Content-Type: application/json; charset=utf-8 \r\n";
request += "Content-Type: application/json \r\n";
request += "Accept: */*\r\n";
request += "Content-Length: ";
request += std::to_string(body.length());
request += "\r\n";
request += "Connection: close\r\n\r\n";
request += body;
}
void
Server::genbody(const std::string& reqtype, Blob& token, Blob& body)
{
ptree requestTree;
ptree requestElement;
std::cout << "Server::genbody" << std::endl;
// Add request token
requestElement.put_value(token);
requestTree.add_child(reqtype, requestElement);
// Create (JSON) request
std::stringstream ss;
write_json(ss, requestTree, false);
body = ss.str();
}
void
Server::send(tcp::socket& sock, boost::asio::io_service& io_service, std::string& request) {
std::cout << "Server::send" << std::endl;
try {
async_write(sock, boost::asio::buffer(request), boost::bind(
&Server::on_send,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
io_service.run();
} catch (exception& e) {
std::cerr << e.what() << std::endl;
}
return;
}
void
Server::on_send(const boost::system::error_code & error, size_t bytes_transferred) {
std::cout << "Server::on_send" << std::endl;
if (error) {
std::cout << "Error: " << error.message() << std::endl;
std::cout << "Bytes sent: " << bytes_transferred << std::endl;
} else {
std::cout << "Bytes sent: " << bytes_transferred << std::endl;
}
return;
}
int main() {
try {
boost::asio::io_service ioserv;
tcp::acceptor acceptor(ioserv, tcp::endpoint(tcp::v4(), 8888));
boost::shared_ptr<Server> sesh(new Server());
//Blob
Blob token("a");
// Generate URL body
Blob body;
sesh->genbody(NOOBJECT, token, body);
// Generate Headers (append body)
std::string request;
sesh->genheader(body, request);
for (;;) {
tcp::socket newsocket(ioserv);
acceptor.accept(newsocket);
cout << "New Request" << endl;
sesh->send(newsocket, ioserv, request);
}
} catch (exception& e) {
cerr << e.what() << endl;
}
return 0;
}
我已使用REST客户端PAWS验证是否已检索到以下内容:
POST / HTTP/1.1
Host:xx.xx.xx.x:8888
User-Agent: ProteAuth/1.0
Content-Type: application/json; charset=utf-8
Content-Type: application/json
Accept: */*
Content-Length: 10
Connection: close
{"a":"a"}
有谁知道为什么我经常失去与网络的连接(Boost ASIO嵌入式http服务器)?
答案 0 :(得分:1)
我能够确定 async_write 功能( BOOST :: Asio )与之间的JSON数据传输问题> AFHTTPRequestOperationManager ( iOS )。它只是一个形状错误的标题,在genheader函数中更新如下:
void Server::genheader(Blob& body, std::string& request)
{
cout << "Server::genheader" << endl;
request += "HTTP/1.1 200 OK \r\n";
request += "Content-Length: ";
request += std::to_string(body.length());
request += "\r\n";
request += "Content-Type: application/json \r\n";
request += "Connection: Keep-Alive\r\n\r\n";
request += body;
}