需引用这几个程序集
System::Core
Newtonsoft::Json::Linq
System::Net::Http
HttpClientHandler^ handler1 =gcnew HttpClientHandler();
handler1->AllowAutoRedirect = false;
HttpClient^ httpClient1 = gcnew HttpClient(handler1);
httpClient1->DefaultRequestHeaders->Add("user-agent", "IE11");
try
{
Task<System::Net::Http::HttpResponseMessage^>^ task = httpClient1-
>GetAsync("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp") ;
task->Wait();
if (task->Result->IsSuccessStatusCode)
task->Result->EnsureSuccessStatusCode();
else
{
MessageBox::Show("请求没有成功!", "错误提示");
return;
}
HttpResponseMessage^ response = task->Result;
Task<System::String^>^ result = respon se->Content->ReadAsStringAsync();
String^ responseBodyAsText = result->Result;
MessageBox::Show(responseBodyAsText, "成功请求");
JObject^ sertime = (gcnew JObject())->Parse(responseBodyAsText) ;
//服务器响应的正文 {"api":"mtop.common.getTimestamp", "v" : "*", "ret" : ["SUCCESS::
接口调用成功"], "data" : {"t":"1549701942078"}}
JToken^ data= sertime["data"] ;
JToken^ t = data ["t"];
MessageBox::Show(t->ToString(), "淘宝当前时间");
// handler1->Dispose(); C++不能执行
// httpClient1->Dispose();
return;
}
catch (HttpRequestException^ hre)
{