Using HttpGet returning complete HTML code

会有一股神秘感。 提交于 2019-12-30 11:33:28

问题


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

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??


回答1:


Try specify Accept & Content-Type in you http header:

httpget.setHeader("Accept", "application/json"); // or application/jsonrequest
httpget.setHeader("Content-Type", "application/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.

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();

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");


来源:https://stackoverflow.com/questions/9805300/using-httpget-returning-complete-html-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!