http - 同样的post请求代码在Java和android中执行结果不同。
ringa_lee
ringa_lee 2017-04-17 11:37:35
0
1
242

同样的代码,在JAVA和android里得到的httpresponse不一样为什么呢?

主要代码如下。

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

FileBody bin = new FileBody(new File(fileName));
StringBody comment = new StringBody("Filename: " + fileName);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

java得到的response内容

android得到的:

ringa_lee
ringa_lee

ringa_lee

reply all(1)
大家讲道理

出现不同结果的原因可能是HttpClient的版本不同造成的,第二个结果也是由HTTP 302响应后重定向到新的URL中的,区别在于Java中默认没有进行重定向。

解决方法就是统一重定向方式

参考:

  • http://stackoverflow.com/questions/5169468/handling-httpclient-redirects

  • http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!