java发送http请求模板示例

来源:互联网 发布:腾讯软件管理下载 编辑:程序博客网 时间:2024/05/21 11:01
package com.shkj.common.http;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;


public class Poster {

/** 日志输出器 */
private static Log log = LogFactory.getLog(Poster.class);

//public static final CloseableHttpClient client = HttpClients.createDefault();
private CloseableHttpClient client;
private String charSet;//字符集
private int timeout;//超时时间
private String application;//目标请求路径

/**
* 无参数post
* @param uri
* @return
*/
public String post(String uri){
return this.post(null, uri, null);
}

/**
* Map<String, String>型参数POST
* @param params
* Map<String, String>型参数
* @param uri
* @return
*/
public String post(Map<String, String> params, String uri){
return this.post(params, uri, null);
}

/**
* Map<String, String[]>型参数POST
* @param uri
* @param params
* Map<String, String[]>型参数
* @return
*/
public String post(String uri, Map<String, String[]> params){
return this.post(null, uri, params);
}

/**
* Map<String, String>型参数
* 及Map<String, String[]>型参数POST
* @param prm2
* Map<String, String>型参数
* @param path
* @param prm1
* Map<String, String[]>型参数
* @return
*/
public String postParamByObject(Map<String, Object> prm1, String path, Map<String, String[]> prm2){
String content = "";
client = HttpClients.createDefault();
try {
if(StringUtils.isEmpty(path)) return null;
String uri = new StringBuilder(application).append("/").append(path).toString();
HttpPost httpPost = new HttpPost(uri);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
httpPost.setConfig(requestConfig);


List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//组装参数
if(null != prm1) {
Set<String> keySet = prm1.keySet();
for(String key : keySet) {
nvps.add(new BasicNameValuePair(key, prm1.get(key).toString()));
}
}
//组装参数
            if(null != prm2) {
           Set<String> keySet = prm2.keySet();
for(String key : keySet) {
String[] vals = prm2.get(key);
if(null == vals) continue;
for(String v : vals) {
           nvps.add(new BasicNameValuePair(key, v));
}
       }
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, charSet));
//POST
CloseableHttpResponse response = client.execute(httpPost);
try {
//获取响应
HttpEntity httpEntity = response.getEntity();  
                if (httpEntity != null) {
                content = EntityUtils.toString(httpEntity, charSet);
                }
                EntityUtils.consume(httpEntity);
            } finally {
                response.close();
            }
}catch(Exception e){
log.error(e.getMessage());
} finally {
try {
client.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
return content;
}


/**
* Map<String, String>型参数
* 及Map<String, String[]>型参数POST
* @param prm2
* Map<String, String>型参数
* @param path
* @param prm1
* Map<String, String[]>型参数
* @return
*/
public String post(Map<String, String> prm1, String path, Map<String, String[]> prm2){
String content = "";
client = HttpClients.createDefault();
try {
if(StringUtils.isEmpty(path)) return null;
String uri = new StringBuilder(application).append("/").append(path).toString();
HttpPost httpPost = new HttpPost(uri);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
httpPost.setConfig(requestConfig);


List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//组装参数
if(null != prm1) {
Set<String> keySet = prm1.keySet();
for(String key : keySet) {
nvps.add(new BasicNameValuePair(key, prm1.get(key)));
}
}
//组装参数
            if(null != prm2) {
           Set<String> keySet = prm2.keySet();
for(String key : keySet) {
String[] vals = prm2.get(key);
if(null == vals) continue;
for(String v : vals) {
           nvps.add(new BasicNameValuePair(key, v));
}
       }
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, charSet));
//POST
CloseableHttpResponse response = client.execute(httpPost);
try {
//获取响应
HttpEntity httpEntity = response.getEntity();  
                if (httpEntity != null) {
                content = EntityUtils.toString(httpEntity, charSet);
                }
                EntityUtils.consume(httpEntity);
            } finally {
                response.close();
            }
}catch(Exception e){
log.error(e.getMessage());
} finally {
try {
client.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
return content;
}


public String getCharSet() {
return charSet;
}


public void setCharSet(String charSet) {
this.charSet = charSet;
}


public int getTimeout() {
return timeout;
}


public void setTimeout(int timeout) {
this.timeout = timeout;
}


public String getApplication() {
return application;
}


public void setApplication(String application) {
this.application = application;
}


}
1 0
原创粉丝点击