1
に送信すると、ローカルサーバーへのユーザーログインをシミュレートしたHTTPリクエストが初期化されました。ここに私が書いたコードがあります。CPPRest SDKがCPPRest SDK(2.8)のテスト中にサーバーへのHTTPリクエストを
void printJSON(json::value v)
{
if (!v.is_null()){
// Loop over each element in the object
for (auto iter = v.as_object().cbegin(); iter != v.as_object().cend(); ++iter){
const string &key = iter->first;
const json::value &value = iter->second;
if (value.is_object() || value.is_array()){
if(key.size() != 0){
std::wcout << "Parent: " << key.c_str() << std::endl;
}
printJSON(value);
if(key.size() != 0){
std::wcout << "End of Parent: " << key.c_str() << std::endl;
}
}else{
std::wcout << "Key: " << key.c_str() << ", Value: " << value.to_string().c_str() << std::endl;
}
}
}
}
void login(){
http_client client("http://localhost:8080/user");
http_request request(methods::POST);
request.headers().add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
request.headers().add("Content-Length", "100");
request.headers().add("Host", "testhost.com");
request.headers().add("X-Requested-With", "XMLHttpRequest");
request.set_body("u_id=test_admin&pwd=123456789");
pplx::task<void> task = client.request(request)
.then([](http_response response)-> pplx::task<json::value>{
if(response.status_code() == status_codes::OK){
return response.extract_json();
} else {
return pplx::task_from_result(json::value());
};})
.then([](pplx::task<json::value> previousTask){
try{
const json::value & v = previousTask.get();
printJSON(v);
} catch(const http_exception &e){
std::cout<<e.what()<<std::endl;
}
});
try{
task.wait();
} catch(std::exception &e){
std::cout<<e.what()<<std::endl;
}
}
私はこのコードを実行すると、何も起こらなかった、要求がJSPを使用してテストされているサーバに到達したことがないようだ、だから私は何かが私のコードで間違っていたかなり確信しています。助けてください、ありがとう