我正在尝试对军刀的api进行GET调用。我没有收到 我的回复中的数据,我对httpGet / post / request相当新。
public HttpResponse callGetMethod() {
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("https://api.sabre.com/v1/shop/flights?origin=ATL&destination=LAS&departuredate=2016-08-13&returndate=2016-08-15&limit=1&enabletagging=true"));
response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
我觉得我在某个地方只缺了一小步。
感谢任何帮助!
答案 0 :(得分:1)
您可以尝试这样的事情:
string authoriazionToken = "T1RLAQILfFO0MrYdVVePW8z......";
WebRequest webRequest = WebRequest.Create(new URI("https://api.sabre.com/v1/shop/flights?origin=ATL&destination=LAS&departuredate=2016-08-13&returndate=2016-08-15&limit=1&enabletagging=true"));
webRequest.Method = "GET";
webRequest.Headers.Add("Authorization", "Bearer " + authoriazionToken);
webRequest.Headers.Add("Accept-Encoding", "gzip");
webRequest.ContentType = "application/json";
try
{
WebResponse webResp = webRequest.GetResponse();
using (var reader = new System.IO.StreamReader(webResp.GetResponseStream()))
{
//Insert parsing code here
}
}
catch.....
我有别的东西。