首页 > 同样的post请求代码在Java和android中执行结果不同。

同样的post请求代码在Java和android中执行结果不同。

同样的代码,在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得到的:


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

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

参考:

【热门文章】
【热门文章】