2

サーバーにリクエストを送信してデータを取得する小さな C++ プログラムを作成したいと考えています。C++ Rest-SDK を見つけたので、それを使用することにしました。さまざまな Web サイトでコード例を検索しましたが、それらの多くは機能せず、構文エラーが表示されます。私が今得たのはそのコードですが、 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します。呼び出しwaitの後に追加する必要があります。then

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 に答える