android中模拟http协议表单上传

来源:互联网 发布:广告图设计软件 编辑:程序博客网 时间:2024/05/01 18:34

转自:http://helloandroid.iteye.com/blog/1183853

利用ie浏览器插件httpwatch查看form表单上传时的数据封装格式,然后照着这数据格式自己一步一步封装 



 

 

Java代码  收藏代码
  1. package com.android.cist.network.form;  
  2.   
  3. import java.io.DataOutputStream;  
  4. import java.io.InputStream;  
  5. import java.io.UnsupportedEncodingException;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8. import java.net.URLEncoder;  
  9. import java.util.Iterator;  
  10. import java.util.Map;  
  11. import java.util.Set;  
  12.   
  13. public class HttpFormUtil {  
  14.   
  15.     public static String post(String actionUrl, Map<String, String> params,FormFile[] files) {  
  16.         try {  
  17.             String enterNewline = "\r\n";  
  18.             String fix="--";  
  19.             String boundary="######";  
  20.             String MULTIPART_FORM_DATA = "multipart/form-data";  
  21.               
  22.             URL url = new URL(actionUrl);  
  23.               
  24.             HttpURLConnection con = (HttpURLConnection)url.openConnection();  
  25.             con.setDoInput(true);  
  26.             con.setDoOutput(true);  
  27.             con.setUseCaches(false);  
  28.             con.setRequestMethod("POST");  
  29.             con.setRequestProperty("Connection""Keep-Alive");  
  30.             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, */*");  
  31.             con.setRequestProperty("Accept-Encoding""gzip, deflate");  
  32.             con.setRequestProperty("Charset""UTF-8");  
  33.             con.setRequestProperty("Content-Type", MULTIPART_FORM_DATA+ ";boundary=" + boundary);  
  34.               
  35.             DataOutputStream ds = new DataOutputStream(con.getOutputStream());  
  36.             Set<String> keySet = params.keySet();  
  37.             Iterator<String> it = keySet.iterator();  
  38.               
  39.             while(it.hasNext()){  
  40.                 String key = it.next();  
  41.                 String value = params.get(key);  
  42.                 ds.writeBytes(fix+boundary+enterNewline);  
  43.                 ds.writeBytes("Content-Disposition: form-data; "+"name=\"" + key + "\"" + enterNewline);  
  44.                 ds.writeBytes(enterNewline);  
  45.                 //ds.write(value.getBytes("UTF-8"));  
  46.                 ds.writeBytes(value);//如果有中文乱码,保存改用上面的ds.writeBytes(enterNewline);那句代码  
  47.                 ds.writeBytes(enterNewline);  
  48.             }  
  49.               
  50.             if(files!=null&&files.length>0){  
  51.                 ds.writeBytes(fix+boundary+enterNewline);  
  52.                 ds.writeBytes("Content-Disposition: form-data; "+"name=\"" + files[0].getFormname() + "\"" +"; filename=\""+files[0].getFilname()+"\""+enterNewline);  
  53.                 ds.writeBytes(enterNewline);  
  54.                 ds.write(files[0].getData());  
  55.                 ds.writeBytes(enterNewline);  
  56.             }  
  57.               
  58.             ds.writeBytes(fix+boundary+fix+enterNewline);  
  59.             ds.flush();  
  60.               
  61.             InputStream is = con.getInputStream();  
  62.             int ch;  
  63.             StringBuffer b = new StringBuffer();  
  64.               
  65.             while((ch = is.read()) != -1){  
  66.                 b.append((char)ch);  
  67.             }  
  68.             ds.close();  
  69.               
  70.             return b.toString().trim();  
  71.               
  72.         } catch (Exception e) {  
  73.             throw new RuntimeException(e);  
  74.         }  
  75.     }  
  76.       
  77.     public static String encode(String url) {     
  78.         try {     
  79.             return URLEncoder.encode(url, "UTF-8");     
  80.         } catch (UnsupportedEncodingException ex) {     
  81.             return url;     
  82.         }     
  83.     }     
  84.   
  85. }  

0 0
原创粉丝点击