Android中模拟post表单提交,带参数及文件参数
来源:互联网 发布:maclc如何安装windows 编辑:程序博客网 时间:2024/03/29 04:21
有时候需要通过Android客户端向服务器提交数据,带一些参数、图片或其他文件,那么可以用如下方法,模拟post提交
例如网页中有个表单,如下
<form action="xxx.action" method="post" enctype="multipart/form-data"> <input type="text" name="name" /><br /> <input type="file" name="file" /><br /> <input type="submit" /></form>
我们可以在Android客户端模拟post方式提交,以达到上面的效果
/** * 提交数据 * @param url 服务端地址 * @param param 参数集合 * @param file 图片文件 * @param cookie 登录后返回的cookie,无登录验证可为null * @return 服务器返回结果 * @throws Exception */public static String uploadSubmit(String url, Map<String, String> param,File file, String cookie) throws Exception {DefaultHttpClient httpClient = new DefaultHttpClient();HttpPost post = new HttpPost(url);if(cookie != null && cookie.length()>0){//向头信息添加cookiepost.addHeader("cookie", cookie);}MultipartEntity entity = new MultipartEntity();if (param != null && !param.isEmpty()) {for (Map.Entry<String, String> entry : param.entrySet()) {if (entry.getValue() != null&& entry.getValue().trim().length() > 0) {entity.addPart(entry.getKey(),new StringBody(entry.getValue()));}}}if (file != null && file.exists()) {entity.addPart("file", new FileBody(file));}post.setEntity(entity);HttpResponse response = httpClient.execute(post);int stateCode = response.getStatusLine().getStatusCode();StringBuffer sb = new StringBuffer();if (stateCode == HttpStatus.SC_OK) {HttpEntity result = response.getEntity();if (result != null) {InputStream is = result.getContent();BufferedReader br = new BufferedReader(new InputStreamReader(is));String tempLine;while ((tempLine = br.readLine()) != null) {sb.append(tempLine);}}}post.abort();return sb.toString();}
0 0
- Android中模拟post表单提交,带参数及文件参数
- CURL模拟表单post提交及相关常用参数的使用(包括提交表单同时上传文件)
- CURL模拟表单post提交及相关常用参数的使用(包括提交表单同时上传文件)
- 通过表单form模拟post方式提交多参数实现文件下载
- 如何获取表单提交值 post(地址栏不带参数)或get(地址栏带参数)
- android 文件上传(POST方式模拟表单提交)
- form表单提交时action带参数
- 表单提交带时间参数 出现404
- js控制表单提交带参数
- 模拟post表单提交
- 继续上次的android上传文件携带参数POST提交
- PHP CURL模拟表单post提交文件,curl POST文件
- Android中使用Post带参数请求的方法
- servlet乱码及接收表单提交参数
- 表单提交参数传递及读取
- struts2中form表单使用post方式提交参数(键值对)超过10000的限制
- HttpClien实现使用post方式模拟表单上传大文件和字符参数
- Android客户端提交post参数中文乱码
- Linux挂载移动硬盘、U盘
- MFC对话框增加最大化和最小化按钮
- 携一缕清风静处
- PHP中 xml转数组
- 本土主帅纷纷下课
- Android中模拟post表单提交,带参数及文件参数
- 理解inode
- 微信封杀大量草根公众号
- solr中关于core的管理
- 转载和积累系列 - 内存碎片化问题
- dom4j解析xml字符串实例
- 异速联虚拟打印出来格式不对,如何处理
- 【几何】-Andrew算法求凸包
- Get Form type using javascript in CRM 2011