1.引入httpclient4.0.1.jar;
2.代码如下:
/**
* 文件名:HttpRequestUtil.java
*
* 版本信息:
* 日期:2013-1-14
* Copyright 足下 Corporation 2013
* 版权所有
*
*/
package com.supermap.earth.rims.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
/**
*
* 项目名称:earthquake
* 类名称:HttpRequestUtil
* 类描述:
* 创建人:MaJian
* 创建时间:2013-1-14 上午11:37:54
* 修改人:MaJian
* 修改时间:2013-1-14 上午11:37:54
* 修改备注:
* @version
*
*/
public class HttpRequestUtil {
/**
* 发送HttpRequest,参数只能是String的
* @param name
* @创建人: MaJian
* @创建时间:2013-1-14 上午11:45:41
* @修改人: MaJian
* @修改时间:2013-1-14 上午11:45:41
* @修改备注:
*/
public static void sendURLRequest(String serverUrl,Map map){
serverUrl = "http://localhost:8080/earthquake/rims/comet/sendMsg.do";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverUrl);
try {
if(map != null && map.size() > 0){
MultipartEntity entity = new MultipartEntity();
Set sets = map.entrySet();
Iterator it = sets.iterator();
while(it.hasNext()){
Map.Entry<String,String> entry = (Entry<String, String>)it.next();
String key = entry.getKey();
String value = entry.getValue();
entity.addPart(key, new StringBody(value));
}
post.setEntity(entity);
}
HttpResponse resp = client.execute(post);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}