我已经使用boost :: asio库实现了一个HTTPS客户端,实现很顺利,但是当我发送一个调用某个类似servlet的实体的GET请求时会出现问题。
获取请求
request_stream << "GET "<<"https://172.198.71.135:8085/jrnal_content/test?data=MFBLUQ==&iv=aHU5Rw=="<<" HTTP/1.0\r\n"
请求已成功从我的服务端写入服务器,但我得到的响应是403
(“禁止”),稍后我发送了另一个GET请求。
request_stream << "GET "<<"https://172.198.71.135:8085/users/Sign_in"<<" HTTP/1.0\r\n"
我有200
,我真的不知道我的第一个请求(收到403)出了什么问题,我确信证书和内容没有问题,我从来没有实现过之前的HTTP客户端/服务器,所以我想确定是否正在进行正确的请求调用。
请参阅下面的代码
boost::asio::io_service io_service1;
boost::asio::io_service &io_service(io_service1);
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
boost::asio::ssl::context& context_=ctx;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_(io_service,context_);
int main()
{
context_.set_options(boost::asio::ssl::context::default_workarounds| boost::asio::ssl::context::no_sslv2
| boost::asio::ssl::context::single_dh_use);
context_.set_password_callback(my_password_callback);
context_.use_certificate_chain_file("SSL\\rich.crt");
context_.use_private_key_file("SSL\\rich.key", boost::asio::ssl::context::pem);
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
tcp::resolver resolver_(io_service);
tcp::resolver::query query("172.198.71.135", "https");
tcp::resolver::iterator endpoint_iterator = resolver_.resolve(query);
boost::asio::connect(socket_.lowest_layer(),endpoint_iterator);
socket_.lowest_layer().set_option(tcp::no_delay(true));
socket_.set_verify_mode(boost::asio::ssl::verify_peer);
socket_.set_verify_callback(boost::bind(verify_certificate));//(boost::asio::ssl::rfc2818_verification("172.198.71.135"));
socket_.handshake(boost::asio::ssl::stream_base::client);
boost::asio::placeholders::error, boost::asio::placeholders::iterator));
string path="https://172.198.71.135:8085/jrnal_content/test?";
temp=path+"data="+data+"&"+"iv="+Iv;
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET "+temp+" HTTP/1.0\r\n" ;
request_stream << "Host: " <<"172.198.71.135"<< "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
const char* header=boost::asio::buffer_cast<const char*>(request.data());
cout<<header<<endl;
try{
boost::asio::write(socket_, request);
request_stream.clear();
t=sizeof(request);
request.consume(t);
}catch(runtime_error e)
{ ss<<e.what();
string err=ss.str();
err="";
ss.str("");
}
boost::asio::streambuf response;
try{
boost::asio::read_until(socket_, response, "\r\n");
}catch(runtime_error e)
{
ss<<e.what();
string err=ss.str();
err="";
ss.str("");
}
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
cout<<status_code<<" status_code"<<endl;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTPS/")
{
}
if (status_code==200)
{
ss<<ID;
string SID=ss.str();
ss.str("");
boost::asio::read_until(socket_, response, "\r\n\r\n");
response_stream.clear();
t=sizeof(response);
response.commit(t);
}
else{
Sleep(6000);
continue;
}
Sleep(7000);
}
return 0;
}
string my_password_callback(size_t t, boost::asio::ssl::context_base::password_purpose p)//std::size_t max_length,ssl::context::password_purpose purpose )
{
std::string password;
return "12345";
}
bool verify_certificate()
{
return true;
}
答案 0 :(得分:0)
&#34;三位数的混乱&#34;通常被称为&#34;数字&#34;。在这种情况下,该号码是网络服务器响应的HTTP Status Code。
如果您阅读了#34; HTTP&#34;甚至可能是HTTP响应代码,你会发现它意味着Forbidden:
Web服务器可能会返回 403 Forbidden HTTP状态代码,以响应客户端对网页或资源的请求,以指示可以访问服务器并了解该请求,但拒绝采取进一步行动。状态代码403响应是Web服务器被配置为由于某种原因拒绝客户端访问所请求资源的结果。
确保提供所需的HTTP Authentication