Using HttpGet returning complete HTML code

雨燕双飞 提交于 2019-12-01 12:36:54

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