2

我想编写一个小 C++ 程序,向服务器发送请求并获取一些数据。我找到了 C++ Rest-SDK 并决定使用它。我在不同的网站上搜索了代码示例,但其中许多都不起作用并显示语法错误。我现在得到的是该代码,但跳过了 client.request 方法。该程序永远不会跳进去。希望有人能意识到这个问题,也许可以解释我必须改变什么。

#include <Windows.h>
#include <iostream>
#include <sstream>
#include <string>
#include "cpprest/containerstream.h"
#include "cpprest/filestream.h"
#include "cpprest/http_client.h"
#include "cpprest/json.h"
#include "cpprest/producerconsumerstream.h"
#include "cpprest/http_client.h"
#include <string.h>
#include <conio.h>

using namespace std;
using namespace web;
using namespace web::json;
using namespace web::http;
using namespace web::http::client;
using namespace utility;
using namespace utility::conversions;


int main() {

  http_client client(L"http://httpbin.org/ip");

  client.request(methods::GET).then([](http_response response)
  { 
    if(response.status_code() == status_codes::OK)
    {
      auto body = response.extract_string().get();    
      std::wcout << body;
      getch();
    }
  });


  return 0;
}
4

4 回答 4

5

您的程序运行结束main并终止。您需要waitthen通话后添加:

client.request(methods::GET).then([](http_response response)
{ 
    // ...
}).wait();
于 2014-06-07T00:42:54.347 回答
5

主线程可能在“请求”任务完成之前终止,因此您看不到任何控制台输出。我建议您在“.then”之后调用任务“wait()”函数,就像他们网站上的答案一样

于 2014-04-30T09:36:35.643 回答
1

此代码有效:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
#include "StdAfx.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int main(int argc, char* argv[])
{
  // Make the request and asynchronously process the response.
  http_client client(L"http://localhost:8082/TPJAXRS/Test/test");

  client.request(methods::GET).then([](http_response response){ 
    if(response.status_code() == status_codes::OK){
      auto body = response.extract_string().get();    
      std::wcout << body<< std::endl;
    }});
  std::cout << "Hello world!" << std::endl;
  system("PAUSE");
  return 0;
}
于 2016-04-07T21:25:08.133 回答
0
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h>              // HTTP server
#include <cpprest/json.h>                       // JSON library
#include <cpprest/uri.h>                        // URI library
#include <cpprest/ws_client.h>                  // WebSocket client
#include <cpprest/containerstream.h>            // Async streams backed by                    STL containers
#include <cpprest/interopstream.h>              // Bridges for integrating  Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h>               // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h>     // Async streams for producer consumer scenarios
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams
using namespace web::http::experimental::listener;          // HTTP server
using namespace web::experimental::web_sockets::client;     // WebSockets client
using namespace web::json;                                  // JSON library
int main(int argc, char* argv[])
{
 auto fileStream = std::make_shared<ostream>();

// Open stream to output file.
    pplx::task<void> requestTask =   fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
    *fileStream = outFile;

    // Create http_client to send the request.
    http_client client(U("http://www.bing.com/"));

    // Build request URI and start the request.
    uri_builder builder(U("/search"));
    builder.append_query(U("q"), U("cpprestsdk github"));
    return client.request(methods::GET, builder.to_string());
})

    // Handle response headers arriving.
    .then([=](http_response response)
{
    printf("Received response status code:%u\n", response.status_code());

    // Write response body into the file.
    return response.body().read_to_end(fileStream->streambuf());
})

    // Close the file stream.
    .then([=](size_t)
{
    return fileStream->close();
});

// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
    requestTask.wait();
}
catch (const std::exception &e)
{
    printf("Error exception:%s\n", e.what());
}

return 0;
}

这是用于在 HTTP 请求中设置

HTTP 教程的链接以 获取更多信息,请阅读本教程

于 2016-11-09T09:16:32.930 回答