C++ 在卡萨布兰卡设置基本HTTP身份验证

C++ 在卡萨布兰卡设置基本HTTP身份验证,c++,rest,basic-authentication,casablanca,C++,Rest,Basic Authentication,Casablanca,我正在尝试修改以包含基本HTTP身份验证以访问: auto fileStream=std::make_shared(); //打开流以输出文件。 auto requestTask=fstream::open_ostream(U(“results.html”))。然后([=](ostream outFile) { *fileStream=outFile; //创建http_客户端以发送请求。 http_client_config; 凭证凭证(“用户名”、“密码”); config.set_凭证(c

我正在尝试修改以包含基本HTTP身份验证以访问:

auto fileStream=std::make_shared();
//打开流以输出文件。
auto requestTask=fstream::open_ostream(U(“results.html”))。然后([=](ostream outFile)
{
*fileStream=outFile;
//创建http_客户端以发送请求。
http_client_config;
凭证凭证(“用户名”、“密码”);
config.set_凭证(creds);
http_客户端(U)https://api.prosper.com/(pdf格式),;
//生成请求URI并启动请求。
uri_生成器(U(“/api/Listings/”);
返回client.request(方法::GET,builder.to_string());
})
...
不幸的是,我一直收到错误401-未经授权。但是,我可以通过
https://username:password@api.prosper.com/api/Listings/
,我可以使用卡萨布兰卡访问不需要身份验证的常规网页

一般来说,我对REST和web都是新手,文档是无用的-
http\u client\u config
是“用于设置可能的配置选项”。这不是开玩笑。我甚至不确定我是否使用了正确的类——这些东西看起来是正确的


如何向卡萨布兰卡的http_客户端请求添加基本身份验证?

您需要向请求添加一个标头,其中包含
用户名:密码的
base64
比如说

// Please check how to convert into base64

XYZtaW46Wr6yZW0xMAXY = base64("username:password")

// Creating http_client
http_client_config config;
credentials cred(L"username", L"Password");
config.set_credentials(cred);
http_client client(U("https://serverip"),config);
// create header
http_request req(methods::GET);
// Add base64 result to header
req.headers().add(L"Authorization", L"Basic XYZtaW46Wr6yZW0xMAXY");
req.set_request_uri(L"/api/json/xyz");
pplx::task<http_response> responses = client.request(req);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
wcout << jvalue.get().to_string();
//请检查如何转换为base64
XYZtaW46Wr6yZW0xMAXY=base64(“用户名:密码”)
//创建http_客户端
http_client_config;
凭证凭证(L“用户名”,L“密码”);
配置。设置_凭证(cred);
http_客户端(U)https://serverip(pdf格式),;
//创建标题
http_请求请求(方法::GET);
//将base64结果添加到标头
添加(L“授权”,L“基本XYZtaW46Wr6yZW0xMAXY”);
请求设置请求uri(L)/api/json/xyz);
任务响应=客户端请求(req);
pplx::task jvalue=responses.get().extract_json();

你是个巫师。我使用卡萨布兰卡的conversions::to_base64(“用户名:密码”)来转换字符串。为什么要在配置中添加凭据,还要添加授权标头?一种方法不应该足够吗?至少对我来说,只需应用其中一个(即通过config.set_凭据)就可以了。
// Please check how to convert into base64

XYZtaW46Wr6yZW0xMAXY = base64("username:password")

// Creating http_client
http_client_config config;
credentials cred(L"username", L"Password");
config.set_credentials(cred);
http_client client(U("https://serverip"),config);
// create header
http_request req(methods::GET);
// Add base64 result to header
req.headers().add(L"Authorization", L"Basic XYZtaW46Wr6yZW0xMAXY");
req.set_request_uri(L"/api/json/xyz");
pplx::task<http_response> responses = client.request(req);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
wcout << jvalue.get().to_string();