java模拟HttpRequest

前一段时间,遇到一个需求,需要用java代码了模拟一个http·请求,要可以传递post参数和上传文件。做了些google后,整理如下:


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HttpRequestMaker{
public HttpRequestMaker( String url ) throws Exception {
m_Url = new URL( url );
}

public final static String boundary = "---------------------------7d71b526e00e4";
public final static String prefix = "--";
public final static String newLine = "\r\n";

public final static String fileContentType = "Content-Type: application/octet-stream\r\n\r\n";
public final static String fileContentDisposition = "Content-Disposition: form-data; name=\"%name\"; filename=\"%fileName\"\r\n";
public final static String paramContentDisposition = "Content-Disposition: form-data; name=\"%name\"\r\n\r\n";

private URL m_Url = null;
private URLConnection uc = null;
private Map m_fileMap = new HashMap();
private Map m_paraMap = new HashMap();
long m_FileTotalSize = 0;

public void AddFile(String name, String filename){
File file = new File( filename );
if( file.exists() ){
m_FileTotalSize += file.length();
m_fileMap.put(name, file);
}
}

public void AddFile(String name, File file){
if( file.exists() ){
m_FileTotalSize += file.length();
m_fileMap.put(name, file);
}
}

public void AddParam( String name, String value){
m_FileTotalSize += value.length();
m_paraMap.put(name, value);
}

public void Upload() throws Exception {
try{
uc = m_Url.openConnection();
uc.setDoOutput( true );
uc.setRequestProperty( "Accept", "*/*" );
uc.setRequestProperty( "Accept-Language", "ja" );
uc.setRequestProperty( "Content-Type", "multipart/form-data; boundary=" + boundary);
uc.setRequestProperty( "Accept-Encoding", "gzip, deflate" );
uc.setRequestProperty( "Connection", "Keep-Alive" );
uc.setRequestProperty( "Cache-Control", "no-cache" );

DataOutputStream dos = new DataOutputStream( uc.getOutputStream() );

for(Iterator iterator = m_fileMap.keySet().iterator(); iterator.hasNext();){
String key = (String)iterator.next();
File file = (File)m_fileMap.get(key);
String fcd = fileContentDisposition.replaceAll("%name", key);
fcd = fcd.replaceAll("%fileName", file.getName());

dos.writeBytes(prefix + boundary + newLine);
dos.writeBytes(fcd);
dos.writeBytes(fileContentType);

FileInputStream fstram = new FileInputStream(file);
byte[] buf = new byte[4000];
int len = 0;
while (len != -1) {
len = fstram.read(buf);
if(len>0){
dos.write(buf, 0, len);
}
}
dos.writeBytes(newLine);
fstram.close();
}

for(Iterator iterator = m_paraMap.keySet().iterator();iterator.hasNext();){
String key = (String)iterator.next();
String value = (String)m_paraMap.get(key);

String fcd = paramContentDisposition.replaceAll("%name", key);
dos.writeBytes(prefix + boundary + newLine);
dos.writeBytes(fcd);
dos.writeBytes(value);
dos.writeBytes(newLine);
}

dos.writeBytes(prefix + boundary + prefix + newLine);

dos.close();
}
catch(Exception e){
e.printStackTrace();
throw e;
}
}

public InputStream getInputStream() throws Exception{
if(uc==null){
return null;
}
return uc.getInputStream();
}

public static void main(String[] args) throws Exception {

HttpRequestMaker httpRequestMaker= new HttpFileUpoad("http://192.168.1.117:8080/test");
httpRequestMaker.AddFile("file0", "c:/work/test0.xml");
httpRequestMaker.AddFile("file1", "c:/work/test1.xml");
httpRequestMaker.AddParam("p1", "testvalue");
httpRequestMaker.Upload();
InputStream is = httpRequestMaker.getInputStream(); //
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
while((i = bis.read()) != -1){
baos.write(i);
}
System.out.println(baos.toString());

bis.close();
is.close();
}

/**
*
* @param timeout
*/
public void setReadTimeout(int timeout){
if(uc == null){
return;
}
uc.setReadTimeout(timeout);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值