通过C#使用C++boost.asio Rest API
Consume C++ boost.asio Rest API by C#
我有一个正在返回正确数据的rest 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();
我似乎不理解这个问题,因为它很容易从网络浏览器中消费,但它从这个代码中返回502(坏网关)。
您只是在向套接字中写入,响应中没有HTTP,浏览器是"好";足以发现这种问题。您需要使用HTTP协议格式化代码。
请检查HTTP 1.1的RFC 2616(我认为这是最常见的版本)。
相关文章:
- Microsoft C++ 用于图形 API 的 Rest SDK
- Visual C++ and REST API
- QT 和 JIRA Rest API: /rest/api/2/issue/createmeta 意外返回韩语的 iss
- 用于连接在线 REST 服务器的 REST API
- 如何将 JSON 数据发送到 REST API?
- 使用 REST SDK 将图像从 OpenCV 3 发送到认知人脸 API C++
- 使用boost :: Beast进行CPU重的REST API,我是否应该使用异步或同步方式来实现它们以期望延迟
- 在 Firebase 中使用 Rest api(Casablanca c++) 添加子项时遇到问题
- 如何将QnetworkManager用于REST API
- 如何以编程方式为应用程序创建文件夹,以使用 BOX REST API 在其中上传内容
- 通过C#使用C++boost.asio Rest API
- 为瘦客户端设计客户端(厚)/服务器+额外的REST API
- c++ lib 与初学者的 REST API 配合使用
- 如何使用 JSON 实现带有 POCO C++ 网络库的 REST API 服务器
- 如何在卡萨布兰卡 REST API 中检索授权标头
- 使用OneDrive Rest API进行批量下载
- 在Windows上使用OpenSSL访问带有身份验证的REST API
- 如何在xcode中使用curl和c++的rest API
- 如何实现REST API服务器
- 使用REST API的JSON分阶段