Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <string>
- #include <cpprest\http_msg.h>
- #include <cpprest\http_client.h>
- #include <cpprest\uri_builder.h>
- #include <conio.h>
- using namespace std;
- using namespace ::utility;
- using namespace ::utility::conversions;
- auto serviceUsername = U("cpprest@taxcom.ru");
- auto servicePassword = U("123");
- bool enableProxy = false;
- auto proxyHost = U("localhost"); // Set your value please
- auto proxyPort = 8888; // Set your value please
- web::http::client::credentials proxyLoginPassword(U("1"), U("1")); // Set your value please
- void initConfig(web::http::client::http_client_config& config)
- {
- config.set_chunksize(64 * 1024);
- config.set_timeout(std::chrono::minutes(5));
- if (!enableProxy)
- return;
- web::uri_builder builder;
- builder.set_host(proxyHost);
- builder.set_port(proxyPort);
- web::http::client::web_proxy proxy(builder.to_uri());
- proxy.set_credentials(proxyLoginPassword);
- config.set_proxy(proxy);
- }
- string_t login()
- {
- string_t token;
- auto uri = U("http://exw2k8r2/ExchangeAuth/api");
- auto X_TX_Username = U("X-TX-Username");
- auto X_TX_Password = U("X-TX-Password");
- auto User_Agent = U("User-Agent");
- web::http::client::http_client_config config;
- initConfig(config);
- web::http::client::http_client _client(uri, config);
- web::http::http_request req(web::http::methods::GET);
- req.set_request_uri(U("login"));
- req.headers().add(X_TX_Username, serviceUsername);
- req.headers().add(X_TX_Password, servicePassword);
- req.headers().add(User_Agent, U(""));
- token = _client.request(req).then([](pplx::task<web::http::http_response> previousTask)
- {
- try
- {
- auto response = previousTask.get();
- auto status = response.status_code();
- wcout << status << endl;
- if (response.status_code() == web::http::status_codes::OK)
- {
- auto json = response.extract_json().get();
- auto token = json.at(U("authtoken")).as_string();
- return token;
- }
- }
- catch (const web::http::http_exception& e)
- {
- auto desc2 = e.error_code().message();
- cout << desc2 << endl;
- }
- return string_t(U(""));
- }).get();
- wcout << token << endl;
- if (token.empty())
- {
- _getch();
- exit(-1);
- }
- return U("TaxcomToken token=\"") + token + U("\", realm=\"Exchange\"");
- }
- void getMessages(string_t Authorization)
- {
- auto uri = U("http://exw2k8r2/Exchange/api");
- web::http::client::http_client_config config;
- initConfig(config);
- web::http::client::http_client _client(uri, config);
- web::http::http_request req(web::http::methods::GET);
- req.headers().add(web::http::header_names::authorization, Authorization);
- auto User_Agent = U("User-Agent");
- req.set_request_uri(U("messageList"));
- req.headers().add(User_Agent, U(""));
- _client.request(req).then([](pplx::task<web::http::http_response> previousTask)
- {
- try
- {
- auto response = previousTask.get();
- auto status = response.status_code();
- auto desc = response.extract_string().get();
- wcout << status << endl
- << desc << endl;
- }
- catch (const web::http::http_exception& e)
- {
- auto desc2 = e.error_code().message();
- cout << desc2 << endl;
- throw;
- }
- }).get();
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout << "Try connect to server without proxy" << endl;
- try
- {
- auto Authorization = login(); //200 OK
- //Second request with special authorization token with req.headers().add(web::http::header_names::authorization, Authorization) :
- getMessages(Authorization); //200 OK
- }
- catch (...)
- {
- cout << "Can't connect to server" << endl;
- }
- _getch();
- cout << "Try connect to server with proxy" << endl;
- enableProxy = true;
- try
- {
- auto Authorization = login(); //200 OK
- //Second request with special authorization token with req.headers().add(web::http::header_names::authorization, Authorization) :
- getMessages(Authorization); //Bug Unauthorized 401 only on XP
- }
- catch (...)
- {
- cout << "Can't connect to server" << endl;
- }
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement