小编geh*_*geh的帖子

Java HttpURLConnection VS Android HttpURLConnection

我有这个在Windows VM上运行的简单代码:

URL url = new URL("http:xxx.xxx.xxx.xxx/api/cities.json");
HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
connexion.setRequestMethod("GET");

System.out.println("response " + connexion.getResponseCode());
connexion.connect();

InputStream is = connexion.getInputStream();

for (String header : connexion.getHeaderFields().keySet()) {
    System.out.println(header + ":" + connexion.getHeaderField(header));
}
Run Code Online (Sandbox Code Playgroud)

给这些标题

response 200
null:HTTP/1.1 200 OK
X-Frame-Options:SAMEORIGIN
Access-Control-Allow-Origin:*
Vary:Accept-Encoding
Date:Sun, 23 Mar 2014 03:00:06 GMT
Content-Length:1894
Connection:keep-alive
Content-Type:application/json
Server:nginx
Run Code Online (Sandbox Code Playgroud)

Android上的完全相同的代码提供了这些标题

03-22 23:10:35.405: I/System.out(23707): null:HTTP/1.1 200 OK
03-22 23:10:35.405: I/System.out(23707): Access-Control-Allow-Origin:*
03-22 23:10:35.405: I/System.out(23707): Connection:keep-alive
03-22 23:10:35.405: I/System.out(23707): Content-Type:application/json
03-22 23:10:35.405: I/System.out(23707): Date:Sun, 23 Mar 2014 03:10:35 …
Run Code Online (Sandbox Code Playgroud)

java android httpurlconnection

6
推荐指数
1
解决办法
1841
查看次数

标签 统计

android ×1

httpurlconnection ×1

java ×1