论坛首页 移动开发技术论坛

android中模拟http协议表单上传

浏览 6130 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-09-30   最后修改:2011-09-30





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;   
        }   
    }   

}


  • 大小: 40.3 KB
  • 大小: 178.5 KB
  • 大小: 105.7 KB
   发表时间:2011-09-30  
lz做这个是为了?。
0 请登录后投票
   发表时间:2011-09-30  
q6684273 写道
lz做这个是为了?。

android中提交表单数据
0 请登录后投票
   发表时间:2011-10-08  
这个工具是什么
0 请登录后投票
   发表时间:2011-10-08  
mzba520 写道
这个工具是什么

是HttpWatch
0 请登录后投票
   发表时间:2011-10-08  
楼主用httpclient应该会更方便的
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics