Advertisement
Guest User

Bug 401 when using manually specified proxy on XP

a guest
Apr 14th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <cpprest\http_msg.h>
  4. #include <cpprest\http_client.h>
  5. #include <cpprest\uri_builder.h>
  6. #include <conio.h>
  7.  
  8. using namespace std;
  9. using namespace ::utility;
  10. using namespace ::utility::conversions;
  11.  
  12.  
  13. auto serviceUsername = U("cpprest@taxcom.ru");
  14. auto servicePassword = U("123");
  15.  
  16. bool enableProxy = false;
  17.  
  18. auto proxyHost = U("localhost");                                    // Set your value please
  19. auto proxyPort = 8888;                                              // Set your value please
  20. web::http::client::credentials proxyLoginPassword(U("1"), U("1"));  // Set your value please
  21.  
  22. void initConfig(web::http::client::http_client_config& config)
  23. {
  24.     config.set_chunksize(64 * 1024);
  25.     config.set_timeout(std::chrono::minutes(5));
  26.     if (!enableProxy)
  27.         return;
  28.     web::uri_builder builder;
  29.     builder.set_host(proxyHost);
  30.     builder.set_port(proxyPort);
  31.     web::http::client::web_proxy proxy(builder.to_uri());
  32.     proxy.set_credentials(proxyLoginPassword);
  33.     config.set_proxy(proxy);
  34. }
  35.  
  36. string_t login()
  37. {
  38.     string_t token;
  39.     auto uri = U("http://exw2k8r2/ExchangeAuth/api");
  40.     auto X_TX_Username = U("X-TX-Username");
  41.     auto X_TX_Password = U("X-TX-Password");
  42.     auto User_Agent = U("User-Agent");
  43.     web::http::client::http_client_config config;
  44.     initConfig(config);
  45.     web::http::client::http_client _client(uri, config);
  46.     web::http::http_request req(web::http::methods::GET);
  47.     req.set_request_uri(U("login"));
  48.     req.headers().add(X_TX_Username, serviceUsername);
  49.     req.headers().add(X_TX_Password, servicePassword);
  50.     req.headers().add(User_Agent, U(""));
  51.     token = _client.request(req).then([](pplx::task<web::http::http_response> previousTask)
  52.     {
  53.         try
  54.         {
  55.             auto response = previousTask.get();
  56.             auto status = response.status_code();
  57.             wcout << status << endl;
  58.             if (response.status_code() == web::http::status_codes::OK)
  59.             {
  60.                 auto json = response.extract_json().get();
  61.                 auto token = json.at(U("authtoken")).as_string();
  62.                 return token;
  63.             }
  64.         }
  65.         catch (const web::http::http_exception& e)
  66.         {
  67.             auto desc2 = e.error_code().message();
  68.             cout << desc2 << endl;
  69.         }
  70.         return string_t(U(""));
  71.     }).get();
  72.     wcout << token << endl;
  73.     if (token.empty())
  74.     {
  75.         _getch();
  76.         exit(-1);
  77.     }
  78.     return U("TaxcomToken token=\"") + token + U("\", realm=\"Exchange\"");
  79. }
  80.  
  81. void getMessages(string_t Authorization)
  82. {
  83.     auto uri = U("http://exw2k8r2/Exchange/api");
  84.     web::http::client::http_client_config config;
  85.     initConfig(config);
  86.     web::http::client::http_client _client(uri, config);
  87.     web::http::http_request req(web::http::methods::GET);
  88.     req.headers().add(web::http::header_names::authorization, Authorization);
  89.     auto User_Agent = U("User-Agent");
  90.     req.set_request_uri(U("messageList"));
  91.     req.headers().add(User_Agent, U(""));
  92.     _client.request(req).then([](pplx::task<web::http::http_response> previousTask)
  93.     {
  94.         try
  95.         {
  96.             auto response = previousTask.get();
  97.             auto status = response.status_code();
  98.             auto desc = response.extract_string().get();
  99.             wcout << status << endl
  100.                 << desc << endl;
  101.         }
  102.         catch (const web::http::http_exception& e)
  103.         {
  104.             auto desc2 = e.error_code().message();
  105.             cout << desc2 << endl;
  106.             throw;
  107.         }
  108.     }).get();
  109. }
  110.  
  111. int _tmain(int argc, _TCHAR* argv[])
  112. {
  113.     cout << "Try connect to server without proxy" << endl;
  114.     try
  115.     {
  116.         auto Authorization = login();   //200 OK
  117.         //Second request with special authorization token with req.headers().add(web::http::header_names::authorization, Authorization) :
  118.         getMessages(Authorization);     //200 OK
  119.     }
  120.     catch (...)
  121.     {
  122.         cout << "Can't connect to server" << endl;
  123.     }
  124.     _getch();
  125.     cout << "Try connect to server with proxy" << endl;
  126.     enableProxy = true;
  127.     try
  128.     {
  129.         auto Authorization = login();   //200 OK
  130.         //Second request with special authorization token with req.headers().add(web::http::header_names::authorization, Authorization) :
  131.         getMessages(Authorization);     //Bug Unauthorized 401 only on XP
  132.     }
  133.     catch (...)
  134.     {
  135.         cout << "Can't connect to server" << endl;
  136.     }
  137.     _getch();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement