package com.jetsum.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class HttpClientUtil {
private static String JETSUM_PLATFORM_SERVER = null;
public static String JETSUM_PLATFORM_BEANMONITOR = "0";// 0:不监听service
public static String JETSUM_PLATFORM_REQUESTMONITOR = "0";// 0:不监听request
static {
try {
PropertiesUtil.load("/remoteServices.properties");
JETSUM_PLATFORM_SERVER = PropertiesUtil.get("remoteServerAddress");
} catch (Exception e) {
System.out.println("读取平台配置文件出错! 异常是:"+e);
}
}
/**
* 远程调用服务POST方法
* @param url 服务地址
* @param map 传递参数
* @return 服务器返回点JSON字符串
* @throws ClientProtocolException
* @throws IOException
*/
public static String remotePost(String url, Map<String, String> map) throws ClientProtocolException, IOException {
url = JETSUM_PLATFORM_SERVER+url;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> formparams = setHttpParams(map);
UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(param);
HttpResponse response = httpclient.execute(httppost);
String httpEntityContent = GetHttpEntityContent(response);
httppost.abort();
return httpEntityContent;
}
/**
* HTTP DELETE方法进行删除操作
* @param url
* @param map
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String remoteDelete(String url, Map<String, String> map) throws ClientProtocolException, IOException{
url = JETSUM_PLATFORM_SERVER+url;
HttpClient httpclient = new DefaultHttpClient();
HttpDelete httpdelete= new HttpDelete();
List<NameValuePair> formparams = setHttpParams(map);
String param = URLEncodedUtils.format(formparams, "UTF-8");
httpdelete.setURI(URI.create(url + "?" + param));
HttpResponse response = httpclient.execute(httpdelete);
String httpEntityContent = GetHttpEntityContent(response);
httpdelete.abort();
return httpEntityContent;
}
/**
* HTTP PUT方法进行修改操作
* @param url
* @param map
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String remotePut(String url, Map<String, String> map) throws ClientProtocolException, IOException{
url = JETSUM_PLATFORM_SERVER+url;
HttpClient httpclient = new DefaultHttpClient();
HttpPut httpput = new HttpPut(url);
List<NameValuePair> formparams = setHttpParams(map);
UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8");
httpput.setEntity(param);
HttpResponse response = httpclient.execute(httpput);
String httpEntityContent = GetHttpEntityContent(response);
httpput.abort();
return httpEntityContent;
}
/**
* 远程调用服务GET方法
* @param url 服务地址
* @param map 传递参数,参数中如有中文需要客户端转码
* @return 服务器返回点JSON字符串
* @throws ClientProtocolException
* @throws IOException
*/
public static String remoteGet(String url, Map<String, String> map) throws ClientProtocolException, IOException {
url = JETSUM_PLATFORM_SERVER+url;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet();
List<NameValuePair> formparams = setHttpParams(map);
String param = URLEncodedUtils.format(formparams, "UTF-8");
httpget.setURI(URI.create(url + "?" + param));
HttpResponse response = httpclient.execute(httpget);
String httpEntityContent = GetHttpEntityContent(response);
httpget.abort();
return httpEntityContent;
}
/**
* 设置请求参数
* @param map
* @return
*/
private static List<NameValuePair> setHttpParams(Map<String, String> map) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> set = map.entrySet();
for (Map.Entry<String, String> entry : set) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
return formparams;
}
/**
* 获得响应HTTP实体内容
* @param response
* @return
* @throws IOException
* @throws UnsupportedEncodingException
*/
private static String GetHttpEntityContent(HttpResponse response)
throws IOException, UnsupportedEncodingException {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = br.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {
sb.append(line + "\n");
line = br.readLine();
}
return sb.toString();
}
return "";
}
}
Apach HttpClient
最新推荐文章于 2023-01-05 13:50:52 发布