使用HTTPGET返回完整的HTML code [英] Using HttpGet returning complete HTML code
问题描述
我试图调用一个私有网络服务,其中有一个环节我已经使用 GET
方法来访问。在使用浏览器的直接URL(需要先登录),我得到的JSON格式的数据。我调用的URL是这样的
I am trying to invoke a private web-service in which there's one link I've to access using GET
method. While using the direct URL on browser (needs to login first), I get the data in JSON format. The URL I am invoking is like this
http://www.example.com/trip/details/860720?format=json
网址工作正常,但是当我调用此使用 HTTPGET
,我得到的,而不是JSON字符串的网页的HTML代码。在code我现在用的就是如下:
The url is working fine, but when I invoke this using HttpGet
, I am getting the HTML coding of the webpage, instead of the JSON String. The code I am using is as follows:
private String runURL(String src,int id) { //src="http://www.example.com/trip/details/"
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(src);
String responseBody="";
BasicHttpParams params=new BasicHttpParams();
params.setParameter("domain", token); //The access token I am getting after the Login
params.setParameter("format", "json");
params.setParameter("id", id);
try {
httpget.setParams(params);
HttpResponse response = httpclient.execute(httpget);
responseBody = EntityUtils.toString(response.getEntity());
Log.d("runURL", "response " + responseBody); //prints the complete HTML code of the web-page
} catch (Exception e) {
e.printStackTrace();
}
return responseBody;
}
您可以告诉我,我究竟做错了什么?
Can you tell me what am I doing wrong here??
推荐答案
尝试指定接受并内容类型在你的HTTP标头:
Try specify Accept & Content-Type in you http header:
httpget.setHeader("Accept", "application/json"); // or application/jsonrequest
httpget.setHeader("Content-Type", "application/json");
请注意,您可以使用工具,如 Wireshark的捕获和分析收入和结果的HTTP包,并找出的确切风格HTTP标头返回,从一个标准的浏览器的JSON响应。
Note that you can use tools like wireshark capture and analyse the income and outcome http package, and figure out the exact style of the http header that returns your json response from a standard browser.
更新:
你提到需要登录后才可以使用浏览器时,返回的HTML内容可能是登录页面(如果使用基本身份验证类型,则返回一个状态code 401简短的HTML的响应,所以现代的浏览器知道如何处理,更具体,弹出登录提示用户),所以第一次尝试将检查你的HTTP响应状态code:
Update:
You mentioned need login first when using browser, the html content returned is probably the login page (if use basic authentication type, it returns a short html response with status code 401, so a modern browser knows how to handle, more specifically, pop up login prompt to user), so the first try would be checking the status code of your http response:
int responseStatusCode = response.getStatusLine().getStatusCode();
依靠什么样的,你使用的身份验证类型,则可能需要指定您的HTTP请求登录凭据为好,这样的事情(如果它是一个基本的身份验证):
Depend on what kind of authentication type you use, you probably need specify login credentials in your http request as well, something like this (if it is a basic authentication):
httpClient.getCredentialsProvider().setCredentials(
new AuthScope("http://www.example.com/trip/details/860720?format=json", 80),
new UsernamePasswordCredentials("username", "password");
这篇关于使用HTTPGET返回完整的HTML code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!