为什么HttpClient请求返回400,URL请求返回302正常?
今天在做http请求的时候,发现了一个很别扭的地方。
我使用android在请求广告资源的时候,返回给我400(),我以为是服务的问题。
没太在意,可以后来用浏览器打开这个地址,发现浏览器返回的code是302,然后再跳一次跳转就可以跳转到200正常的界面。
所以很纳闷。
后来用URL进行请求,发现也能返回正常的code:200
很晕。
URL请求:
HttpURLConnection openConnection = (HttpURLConnection) u.openConnection();
int responseCode = openConnection.getResponseCode();
InputStream inputStream = openConnection.getInputStream();
System.out.println(responseCode);
Client请求:
HttpClient client=new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
url地址:
http://mobads.baidu.com/ad.html?url=http%3A%2F%2Fm.baidu.com%2Fcpro.php%3Fa0fK000cFDtp14ipxciFcucWgFoHy2H9iL-QuuIcNAkYIMRiaPQiWnmR6W2aNPD15t3bHYIBjLSFbvjfpThNBsKZLbZAU51-HXSkBpuqvKBXOMaqOkbYINsLI1jq.Db_ipx2roRhF596OlZextW6H7sEyFBOg_vTyyxZ-9k1QjPakblN0.IgF_5y9YIZ0lQzqLILT8mh7GuZR8mvqVQsKYUHYs0Zwd5H00IvsqP6KWUMw85yFhrj61myDLgvPsT6KYI1Yz0A7bmvk9TLnqn0KzT1Ys0APYUHdBmy-bIRPdTLw2Tvq80A49IZRqr0KGIA-b5HD10A-Ypy4hUv-b5H00uLKGujYs0ZF-uMKGujYs0APsThqGujY0mMNbuvNYgvN3TA-b5H00my-s5HNGU17hPWPNmvP80ZNGTjd9pAPEPAkRn-Pzu6KWpjY0IA7z5Hc0uZws5HD0TvN_UANzgv-b5Hc0pgPxmgKs5HD0mgKsgv-b5HnznHn1P1mkPWT0mLN1IjdlpZN9UyqB0A4-I7qYXgK-5HD0Uhqs5Hfvnj0s0A7bmhkEmvVxpvN45H00pgPxIv-zuyk-TLnqnfKLmgKxIZ-suHYs0AdEmh-_uNqsUA7YuhqzUHYz0AdETdqvugcqnB31QW00TAsqn0K_XAcqn0KVm1YkuHfYmhn3rfKVIWYk0A4vTjYsQW0snj0snj0s0AT45HD0uh-zTLwxThNMpyq85Hc0TvNWUv4bgLF-uv-EUWY3n1f0TLPs5Hc0TLPsnWYs0ZwYTjY10AwGTLws5H00mycqn0KWThnqnW64PHf0%26__mobads_cookie%3D%26__mobads_sid%3D723ff9d8572510f2&sn=863637021737296&clk=1
------解决方案--------------------GET方式提交的数据超长了,不要用GET方式。换成POST方式提交。
把URL的参数提取出来,不要写到URL里面。
------解决方案--------------------是不是如楼上说的,长度超限制了。Get方式长度限制为1024个字节。
------解决方案--------------------首先复制你的url访问直接跳转到了:http://m.shgene.net/#WZwenzi
URL访问输出:200
没有LZ说的400跟302
------解决方案--------------------我在用android开发时也碰到过相似情况,通过HttpURLConnection获取网页内容时,发出请求一直返回400 。后来查下来是服务端跳转了,设置setInstanceFollowRedirects为false就行。不清楚你是不是类似的原因。
------解决方案--------------------同3楼,浏览器直接是200OK
------解决方案--------------------
public static void main(String[] args) {
try {
String s = "http://mobads.baidu.com/ad.html?url=http%3A%2F%2Fm.baidu.com%2Fcpro.php%3Fa0fK000cFDtp14ipxciFcucWgFoHy2H9iL-QuuIcNAkYIMRiaPQiWnmR6W2aNPD15t3bHYIBjLSFbvjfpThNBsKZLbZAU51-HXSkBpuqvKBXOMaqOkbYINsLI1jq.Db_ipx2roRhF596OlZextW6H7sEyFBOg_vTyyxZ-9k1QjPakblN0.IgF_5y9YIZ0lQzqLILT8mh7GuZR8mvqVQsKYUHYs0Zwd5H00IvsqP6KWUMw85yFhrj61myDLgvPsT6KYI1Yz0A7bmvk9TLnqn0KzT1Ys0APYUHdBmy-bIRPdTLw2Tvq80A49IZRqr0KGIA-b5HD10A-Ypy4hUv-b5H00uLKGujYs0ZF-uMKGujYs0APsThqGujY0mMNbuvNYgvN3TA-b5H00my-s5HNGU17hPWPNmvP80ZNGTjd9pAPEPAkRn-Pzu6KWpjY0IA7z5Hc0uZws5HD0TvN_UANzgv-b5Hc0pgPxmgKs5HD0mgKsgv-b5HnznHn1P1mkPWT0mLN1IjdlpZN9UyqB0A4-I7qYXgK-5HD0Uhqs5Hfvnj0s0A7bmhkEmvVxpvN45H00pgPxIv-zuyk-TLnqnfKLmgKxIZ-suHYs0AdEmh-_uNqsUA7YuhqzUHYz0AdETdqvugcqnB31QW00TAsqn0K_XAcqn0KVm1YkuHfYmhn3rfKVIWYk0A4vTjYsQW0snj0snj0s0AT45HD0uh-zTLwxThNMpyq85Hc0TvNWUv4bgLF-uv-EUWY3n1f0TLPs5Hc0TLPsnWYs0ZwYTjY10AwGTLws5H00mycqn0KWThnqnW64PHf0%26__mobads_cookie%3D%26__mobads_sid%3D723ff9d8572510f2&sn=863637021737296&clk=1";
URL u = new URL(s);
HttpURLConnection openConnection = (HttpURLConnection) u.openConnection();
int responseCode = openConnection.getResponseCode();
InputStream inputStream = openConnection.getInputStream();
System.out.println(responseCode);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
测试结果200
------解决方案--------------------中间有过重新 redirect.
------解决方案--------------------看看你浏览器对缓存的处理是如何设置的。
------解决方案--------------------浏览器 设置 问题吧
------解决方案--------------------不明帮顶!UP!
------解决方案--------------------浏览器正常返回200
其他没试。有可能是服务有问题。这种问题最难查了,无法重现。
可以测试下服务的连通性。
------解决方案--------------------帮楼主问了度娘。是手机浏览器的问题...
------解决方案--------------------#WZwenzi这种格式是重写的,是你访问http://mobads.baidu.com/ad.html?url=http%3A%2F%2Fm.baidu.com%2Fcpro.php%3Fa0fK000cFDtp14ipxciFcucWgFoHy2H9iL-QuuIcNAkYIMRiaPQiWnmR6W2aNPD15t3bHYIBjLSFbvjfpThNBsKZLbZAU51-HXSkBpuqvKBXOMaqOkbYINsLI1jq.Db_ipx2roRhF596OlZextW6H7sEyFBOg_vTyyxZ-9k1QjPakblN0.IgF_5y9YIZ0lQzqLILT8mh7GuZR8mvqVQsKYUHYs0Zwd5H00IvsqP6KWUMw85yFhrj61myDLgvPsT6KYI1Yz0A7bmvk9TLnqn0KzT1Ys0APYUHdBmy-bIRPdTLw2Tvq80A49IZRqr0KGIA-b5HD10A-Ypy4hUv-b5H00uLKGujYs0ZF-uMKGujYs0APsThqGujY0mMNbuvNYgvN3TA-b5H00my-s5HNGU17hPWPNmvP80ZNGTjd9pAPEPAkRn-Pzu6KWpjY0IA7z5Hc0uZws5HD0TvN_UANzgv-b5Hc0pgPxmgKs5HD0mgKsgv-b5HnznHn1P1mkPWT0mLN1IjdlpZN9UyqB0A4-I7qYXgK-5HD0Uhqs5Hfvnj0s0A7bmhkEmvVxpvN45H00pgPxIv-zuyk-TLnqnfKLmgKxIZ-suHYs0AdEmh-_uNqsUA7YuhqzUHYz0AdETdqvugcqnB31QW00TAsqn0K_XAcqn0KVm1YkuHfYmhn3rfKVIWYk0A4vTjYsQW0snj0snj0s0AT45HD0uh-zTLwxThNMpyq85Hc0TvNWUv4bgLF-uv-EUWY3n1f0TLPs5Hc0TLPsnWYs0ZwYTjY10AwGTLws5H00mycqn0KWThnqnW64PHf0%26__mobads_cookie%3D%26__mobads_sid%3D723ff9d8572510f2&sn=863637021737296&clk=1跳转到另外一个地址,它自己定义的,直接访问跳转后的地址,可能没有环境,或者那个参数造成的。302是跳转。