博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MultipartEntity与UrlEncodedFormEntity区别
阅读量:6970 次
发布时间:2019-06-27

本文共 3100 字,大约阅读时间需要 10 分钟。

  hot3.png

 

详细说明查看:

MultipartEntity与UrlEncodedFormEntity区别

今天在弄安卓项目的时候,碰到一个问题,就是安卓在登录请求服务器的时候,总是报Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 8080) after 90000ms: isConnected failed: ECONNREFUSED (Connection refused) ,一开始的时候,我将post请求换成get 请求,都不行,我以为还是安卓权限什么的问题呢,后来经过排除发现不是,在浏览器中输入webservice地址就可以成功,但是在服务器经过JUnit 测试 发现不能请求,此时将注意力从安卓转移到了服务器上,

开始的时候封装的方法是这样写的:

public static InputStream post(String url, MultipartEntity parameters) {

HttpClient client = new DefaultHttpClient();

HttpPost postrequest = new HttpPost(url);
try {
if (parameters != null) {
postrequest.setEntity(parameters);
}
HttpResponse postresponse = client.execute(postrequest);
InputStream is = postresponse.getEntity().getContent();
return is;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

 

后来发现改下面这种就可以访问成功:

 

public static InputStream post(String url, List<NameValuePair> parameters) {

HttpClient client = new DefaultHttpClient();

HttpPost postrequest = new HttpPost(url);
try {
if (parameters != null) {
UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters);
//                postrequest.setEntity(parameters);
postrequest.setEntity(formEntiry);
}
HttpResponse postresponse = client.execute(postrequest);
InputStream is = postresponse.getEntity().getContent();
return is;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

 

其中,主要是MultipartEntity与UrlEncodedFormEntity参数不同

经过在网上查询资料发现,这两个类均实现了HttpEntity接口,而二者的区别就和html表单有关系,

html中的form 表单有两种:除了传统的application/x-www-form-urlencoded表单,我们另一个经常用到的是上传文件用的表单,这种表单的 类型为multipart/form-data。  后者主要是用来上传文件所用,所以一般情况下,在使用webservice 时,使用UrlEncodedFormEntity 比较多,UrlEncodedFormEntity 可以模拟传统的HTML表单传送POST请求中的参数,

如:html表单如下:

<form action=”http://localhost/index.html” method=”POST”>

<input type=”text” name=”param1″ value=”李三”/>
<input type=”text” name=”param2″ value=”男”/>
<inupt type=”submit” value=”submit”/>
</form>

 

代码如下:

List<NameValuePair> formParams = new ArrayList<NameValuePair>();

formParams.add(new BasicNameValuePair(“param1″, “李三”));
formParams.add(new BasicNameValuePair(“param2″, “男”));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, “UTF-8″);

MultipartEntity  则与form类型为multipart/form-data 对应,如 html from 如下:

 

<form action=”http://localhost/index.html” method=”POST”

enctype=”multipart/form-data”>
<input type=”text” name=”param1″ value=”李三”/>
<input type=”text” name=”param2″ value=”男”/>
<input type=”file” name=”param3″/>
<inupt type=”submit” value=”submit”/>
</form>

代码如下:

MultipartEntity entity = new MultipartEntity();

entity.addPart(“param1″, new StringBody(“李三”, Charset.forName(“UTF-8″)));
entity.addPart(“param2″, new StringBody(“男”, Charset.forName(“UTF-8″)));
entity.addPart(“param3″, new FileBody(new File(“C:\\pic.gif”)));

转载于:https://my.oschina.net/pmroad/blog/357075

你可能感兴趣的文章
mysql导出和导入
查看>>
IT软件创业之 -- 电脑设备买过来都是钱,卖出去都是废铁
查看>>
debian的“chkconfig”和“service”
查看>>
敏捷开发
查看>>
"无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"解决办法...
查看>>
MYSQL远程连接授权
查看>>
安装hadoop图文
查看>>
Nginx的连接处理方法
查看>>
22个开源的PHP框架
查看>>
进桌面点右键就提示内存不能读,再点确定后就自动注销。
查看>>
New Features in Xcode 6
查看>>
反射应用和集合按照某个字段排序
查看>>
NFS基于linux用户和UINX用户的文件共享
查看>>
Perl UTF-8字符开关
查看>>
JAVASE 增强
查看>>
iptables常用选项及应用实例操作
查看>>
SharePoint开发部署WSP解决方案包
查看>>
redis之strings类型及操作
查看>>
C段查询雏形之在Java中反查一个IP上的所有域名(旁站查询)
查看>>
关于NetApp: Data Ontap 7 mode 和cluster mode
查看>>