android中模拟http协议表单上传
来源:互联网 发布:广告图设计软件 编辑:程序博客网 时间:2024/05/01 18:34
转自:http://helloandroid.iteye.com/blog/1183853
利用ie浏览器插件httpwatch查看form表单上传时的数据封装格式,然后照着这数据格式自己一步一步封装
- package com.android.cist.network.form;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- public class HttpFormUtil {
- public static String post(String actionUrl, Map<String, String> params,FormFile[] files) {
- try {
- String enterNewline = "\r\n";
- String fix="--";
- String boundary="######";
- String MULTIPART_FORM_DATA = "multipart/form-data";
- URL url = new URL(actionUrl);
- HttpURLConnection con = (HttpURLConnection)url.openConnection();
- con.setDoInput(true);
- con.setDoOutput(true);
- con.setUseCaches(false);
- con.setRequestMethod("POST");
- con.setRequestProperty("Connection", "Keep-Alive");
- con.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*");
- con.setRequestProperty("Accept-Encoding", "gzip, deflate");
- con.setRequestProperty("Charset", "UTF-8");
- con.setRequestProperty("Content-Type", MULTIPART_FORM_DATA+ ";boundary=" + boundary);
- DataOutputStream ds = new DataOutputStream(con.getOutputStream());
- Set<String> keySet = params.keySet();
- Iterator<String> it = keySet.iterator();
- while(it.hasNext()){
- String key = it.next();
- String value = params.get(key);
- ds.writeBytes(fix+boundary+enterNewline);
- ds.writeBytes("Content-Disposition: form-data; "+"name=\"" + key + "\"" + enterNewline);
- ds.writeBytes(enterNewline);
- //ds.write(value.getBytes("UTF-8"));
- ds.writeBytes(value);//如果有中文乱码,保存改用上面的ds.writeBytes(enterNewline);那句代码
- ds.writeBytes(enterNewline);
- }
- if(files!=null&&files.length>0){
- ds.writeBytes(fix+boundary+enterNewline);
- ds.writeBytes("Content-Disposition: form-data; "+"name=\"" + files[0].getFormname() + "\"" +"; filename=\""+files[0].getFilname()+"\""+enterNewline);
- ds.writeBytes(enterNewline);
- ds.write(files[0].getData());
- ds.writeBytes(enterNewline);
- }
- ds.writeBytes(fix+boundary+fix+enterNewline);
- ds.flush();
- InputStream is = con.getInputStream();
- int ch;
- StringBuffer b = new StringBuffer();
- while((ch = is.read()) != -1){
- b.append((char)ch);
- }
- ds.close();
- return b.toString().trim();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- public static String encode(String url) {
- try {
- return URLEncoder.encode(url, "UTF-8");
- } catch (UnsupportedEncodingException ex) {
- return url;
- }
- }
- }
0 0
- android中模拟http协议表单上传
- Android 模拟HTTP 协议进行表单提交
- Android表单及图片上传--HTTP协议构造分析
- android中图片上传使用http协议
- Android 表单模拟,文件上传!
- Android实现模拟表单上传
- Net模拟http表单文件上传
- Android 模拟表单上传 多图片上传
- 利用socket模拟http的混合表单上传(在一个请求中提交表单并上传多个文件)
- [android]模拟Http表单,实现本地文件(图片等)上传到服务器端
- android 模拟表单文件上传servlet
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- 在Android上通过模拟HTTP multipart/form-data请求协议信息实现图片上传
- 在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- 在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- 试看自己能坚持多久
- Extjs 下拉框没有数据
- MFD还原和备份
- VC连接ACCESS数据库出现的问题
- (十)洞悉linux下的Netfilter&iptables:网络地址转换原理之SNAT
- android中模拟http协议表单上传
- C基础----控制流程
- 【并发编程】ThreadPoolExecutor参数详解
- 求圆的面积1【Java】
- Qt_OpenGL小探1
- android系统字体调到超大后显示不全
- Docker Serials 1: 安装Docker 和 编译Base Image
- Monkey源码分析番外篇之WindowManager注入事件如何跳出进程间安全限制
- Java 使用JSP页面生成随机验证码