【发布时间】:2016-01-21 00:55:31
【问题描述】:
我已使用 AFHTTPRequestOperationManager 成功地将 JSON 数据发送到 BOOST Asio,但未能成功检索 JSON 数据。我确实确保我可以通过 AFNetworking 中的 Reachability 功能联系到主机。我已经尝试了几个基于 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);
}];
但是,我一直收到同样的错误:
Error Domain=NSURLErrorDomain Code=-1005 "网络连接是 丢失。” UserInfo={NSUnderlyingError=0x12fd7c580 {错误 Domain=kCFErrorDomainCFNetwork Code=-1005 "网络连接是 丢失。” UserInfo={NSErrorFailingURLStringKey=http:/xx.xx.xx.x:8888/, _kCFStreamErrorDomainKey=1,NSErrorPeerAddressKey={长度=16,容量=16,字节= 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-embedded http-server)?
【问题讨论】:
标签: json http network-programming afnetworking boost-asio