Http请求在浏览器中返回不同的响应

时间:2013-08-25 23:29:03

标签: java android json http

我从服务器获取JSON对象,当我在浏览器中输入以下生成的URL时,我得到一个带有“num_match”的响应:18,但是当在我的应用程序中运行时,我得到一个带有“num_matches”的JSON对象:2。

URL对象就像这样创建

URL request;
request = new URL(url);

和这样的连接:

HttpURLConnection connection = (HttpURLConnection) request.openConnection();
    connection.setConnectTimeout(MAX_TIME);
    connection.setReadTimeout(MAX_TIME);

url是一个String,我将字符串内容复制到浏览器中进行测试。

字符串是:

http://search.3taps.com/?auth_token=xxxxxxxxxxxxxxxxxx&retvals=heading,body,timestamp,external_url,images,price&rpp=100&source=BKPGE|CRAIG|EBAYC|INDEE|KIJIJ&category=PWSM&radius=200mi&lat=26.244&long=-80.2&annotations={age:18 OR age:19 OR age:20 OR age:21 OR age:22}

URL对象具有以下字段

查询:

auth_token=xxxxxxxxxxxxxxxxxx&retvals=heading,body,timestamp,external_url,images,price&rpp=100&source=BKPGE|CRAIG|EBAYC|INDEE|KIJIJ&category=PWSM&radius=200mi&lat=26.244&long=-80.2&annotations={age:18 OR age:19 OR age:20 OR age:21 OR age:22}

文件:

/?auth_token=xxxxxxxxxxxxxxxxxx&retvals=heading,body,timestamp,external_url,images,price&rpp=100&source=BKPGE|CRAIG|EBAYC|INDEE|KIJIJ&category=PWSM&radius=200mi&lat=26.244&long=-80.2&annotations={age:18 OR age:19 OR age:20 OR age:21 OR age:22}

主机:

search.3taps.com

响应回复为“成功”:两者都是真的,但返回的对象存在差异。我对http不太了解,可能是什么导致了这个?

更新:进一步测试时,注释条目存在时似乎只有一个问题

annotations={age:18 OR age:19 OR age:20 OR age:21 OR age:22}

似乎导致了这个问题。

1 个答案:

答案 0 :(得分:2)

确保在设置服务器的URL时正确编码URL请求。空间,牙箍和冒号都需要适当地逃脱。空格应为%20等。这可能会有所帮助:HTTP URL Address Encoding in Java


旧答案....评论表明这不影响结果......所以向下移动。

服务器很可能正在根据您自己报告的“浏览器”类型更改其行为。连接到HTTP服务器时,您告诉服务器您的UserAgent是什么(通常对于浏览器,它类似于“Internet Explorer ....”或“Mozilla ......”或“Google Chome ...”。服务器将通常会定制用户代理的请求结果(不同的javascript文件和HTML代码会转到IE等)。这也是服务器将移动设备重定向到适合移动设备的网站版本的方式。

很可能服务器正在更改它的响应以匹配您的Java代码公开的UserAgent(通过decault类似于“Java / 1.7.0”。您可以通过几种方式更改此值。看看这个问题Setting user agent of a java URLConnection并尝试使用Mozilla代理程序运行程序,看看是否会得到不同的结果。