同样的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得到的:
请输入图片描述

http java Android httpclient

九十九狐狸 10 years, 7 months ago

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

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

参考:

反叛的舒露露 answered 10 years, 7 months ago

Your Answer