[英]Consume C++ boost.asio Rest API by C#
我有一個返回正確數據的休息 API,問題是當我使用 C# 代碼時。 它返回的壞網關
以下是我的服務器代碼
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
using namespace std;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
int main()
{
try
{
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1313));
for (;;)
{
cout << "listening for socket" << endl;
tcp::socket socket(io_service);
acceptor.accept(socket);
cout << "listening for socket" << endl;
std::string message = make_daytime_string();
message = "I am a response from rest service baby !!!";
boost::system::error_code ignored_error;
boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
為了使用它,我有以下 c# 代碼
WebRequest req = WebRequest.Create("http://192.168.17.85:1313/");
req.Method = "GET";
WebResponse rep = req.GetResponse();
我似乎不明白這個問題,因為它很容易從 Web 瀏覽器中使用,但它從這段代碼中返回 502(壞網關)。
您只是在寫入套接字,您的響應中沒有 HTTP,瀏覽器“足夠好”可以檢測到此類問題。 您需要使用 HTTP 協議格式化您的代碼。
請檢查RFC 2616 for HTTP 1.1(我認為這是最常見的版本)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.