android中模拟http协议表单上传

本文详细介绍了如何使用IE浏览器插件httpwatch查看并理解HTTP Form表单上传时的数据封装格式,通过实例展示了如何根据获取的数据格式进行手动封装。
[color=red]利用ie浏览器插件httpwatch查看form表单上传时的数据封装格式,然后照着这数据格式自己一步一步封装[/color]

[img]http://dl.iteye.com/upload/attachment/563149/6fff3f74-b521-3ce7-9064-79049bc35521.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/563151/3ec4be92-3780-36b0-ae18-ae12e353f954.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/563153/afd61265-e591-3f99-95db-258e0b2d8762.jpg[/img]
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;
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值