java - 在 Android 上运行时,来自网页的 Httpget 不会返回内容

标签 java android apache-httpclient-4.x

我正在尝试获取页面内容:http://www.washingtonpost.com/wp-srv/simulation/simulation_test.json

我能够获取我的java项目中的内容。这是代码:

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);



    BufferedReader rd = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

这会打印我的 Java 项目中的内容。

但是,当我在 Android 项目中执行此代码时,我得到以下返回:

<HTML><HEAD><TITLE>Cisco Systems Inc. Web Authentication Redirect</TITLE><META http-equiv="Cache-control" content="no-cache"><META http-equiv="Pragma" content="no-cache"><META http-equiv="Expires" content="-1"><META http-equiv="refresh" content="1; URL=https://1.1.1.1/login.html?redirect=www.washingtonpost.com/wp-srv/simulation/simulation_test.json"></HEAD></HTML>

我该如何解决这个问题?为什么它只出现在我的Android项目中而不是我的Java项目中?

谢谢

最佳答案

引用android client

假设在 Android 连接到 WIFI 时不使用代理。

假设在 Android 上您没有使用 3G/4G 网络接口(interface)

假设您使用的是 android httpclient 软件包而不是 apache httpclient。

假设 list 已授予 Web 权限。

您必须了解上述内容才能进行更多调试。

当您调用这些时,您将从默认包中获得一堆默认 header :

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);

您需要打开 WIRE & HEADERS 的日志记录,或者需要转储 get 请求的 header ,以便您确切地知道其中的内容。

然后,将您的 Android 生成的 http GET(所有 header )与您在下面看到的curl 测试脚本进行比较。如果您的 Android header 相当接近(不需要精确,则可能会有一些额外的内容,例如“gzip”编码) )到curl headers,然后android就可以工作了。 curl 示例仅显示 3 个 header ,我会大胆猜测您的 Android 客户端默认情况下正在设置 7 或 8 个 header ,并且 WP 服务器不喜欢额外 header 中的某些内容,并且正在重定向到登录。

@ ~$ curl --verbose  http://www.washingtonpost.com/wp-srv/simulation/simulation_test.json


* About to connect() to www.washingtonpost.com port 80 (#0)
*   Trying 23.204.109.48...
* connected
* Connected to www.washingtonpost.com (23.204.109.48) port 80 (#0)
> GET /wp-srv/simulation/simulation_test.json HTTP/1.1
> User-Agent: curl/7.28.1-DEV
> Host: www.washingtonpost.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: webserver
< Content-Type: text/plain
< Last-Modified: Wed, 12 Feb 2014 21:16:22 GMT
< Content-Length: 58187
< ETag: "e34b-52fbe4a6"
< Accept-Ranges: bytes
< Date: Mon, 10 Mar 2014 16:54:42 GMT
< Connection: keep-alive
< 
{
"posts": [

打开 HEADER 日志的 android httpclient 4.3 示例...

D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> PUT /1/classes/MediaItem/XPdLazBUzV HTTP/1.1
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> Content-Length: 90
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> Content-Type: application/json; charset=UTF-8
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> Host: api.parse.com
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> Connection: Keep-Alive
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.5)
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> Accept-Encoding: gzip,deflate
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> X-Parse-Session-Token: li9ds
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> X-Parse-Application-Id: 3qovv
D/ch.boye.httpclientandroidlib.headers(15360): http-outgoing-2 >> X-Parse-REST-API-Key: Theoj
D/

关于java - 在 Android 上运行时,来自网页的 Httpget 不会返回内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22303868/

相关文章:

java - 在 Spring 3.1 中使用基本身份验证的 RestTemplate

java - 无效查询 : URI parse exception in Apache HTTP Client used in Camel

java - 如何使用 Apache HttpClient 获得持久的 HttpConnection?

java - 在没有单击按钮的情况下输入后,将 EditText 的值添加到 strings.xml 中的字符串

android - 使用 HttpURLConnection 的 Android Jelly Bean 4.1 上的 HTTP 基本身份验证问题

java - 我正在为 android 使用 Firebase Apple 登录

android - MediaCodec.dequeueOutputBuffer 在 Android 上编码 h264 时花费很长时间

java - 使用 Java 从文本/字符串中获取特定 URL

java - AppEngine ChannelAPI 重新连接不起作用

java - 如何将 Spring Boot 应用程序项目部署到 Heroku