com.github.jackpanz.spring.util.HttpClientUtil Maven / Gradle / Ivy
The newest version!
package com.github.jackpanz.spring.util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* Created by Administrator on 2016/7/6.
*/
public class HttpClientUtil {
public static String doPost(String url,Map map,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List list = new ArrayList();
Iterator> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry elem = iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
result = ex.getMessage();
}
finally
{
httpPost.releaseConnection();
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy