HttpUtil 跨项目请求

来源:互联网 发布:易石软件怎样用 编辑:程序博客网 时间:2024/06/08 16:38
public class HttpUtil {

private static HttpClient httpClient;
// 默认编码
private static String chaset = "UTF-8";

/**
* 初始化HttpClient实例
*/
static {
httpClient = HttpClients.createDefault();
}

/**
* 获取HttpClient实例
*/
public static HttpClient getHttpClient() {
return httpClient;
}

/**
* get请求(不带参数)
*
* @param url
* 请求url
* @return html 页面数据
*/
public static String get(String url) {
HttpGet httpGet = new HttpGet(url);
String html = null;
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return html;
}

/**
* get请求(不带参数)
*
* @param url
* 请求url
* @param headers
* 请求头
* @return html 页面数据
*/
public static String get(String url, Map<String, String> headers) {
HttpGet httpGet = new HttpGet(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
String html = null;
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return html;
}

/**
* get请求(带参数)
*
* @param url
* 请求url
* @param paramsMap
* get请求参数
* @return html 页面数据
*/
public static String getWithParams(String url, Map<String, String> paramsMap) {
url = url + "?" + parseParams(paramsMap);
HttpGet httpGet = new HttpGet(url);
String html = null;
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return html;
}

/**
* post请求
*
* @param url
* 请求url
* @param paramsMap
* get请求参数
* @return html 页面数据
*/
public static String post(String url, Map<String, String> paramsMap) {
HttpPost httpPost = new HttpPost(url);
String html = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,chaset));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return html;
}

/**
* 设置编码
*
* @param chaset
* @return
*/
public static void setCharset(String chaset) {
HttpUtil.chaset = chaset;
}

/**
* 转换参数列表用于get请求
*
* @param paramsMap
* @return
*/
private static String parseParams(Map<String, String> paramsMap) {
String params = "";
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
params += entry.getKey() + "=" + entry.getValue() + "&";
}
return params.substring(0, params.length() - 1);
}

public static String doDelete(String url){
HttpDelete httpdelete = new HttpDelete(url);
HttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpdelete);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
}catch (Exception ex){
ex.printStackTrace();
}
finally {
httpdelete.releaseConnection();
}
return null;
}

/**
* @Title: httpDelete
* @Description: TODO
* @author: wangyingtao
* @date: 2017/8/21 16:22
* @param url
*/
public static String doDelete(String url,Map<String,String> paramsMap){
try {
URIBuilder builder = new URIBuilder(url);
//设置header
if (paramsMap != null && paramsMap.size() > 0) {
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
builder.setParameter(entry.getKey(), entry.getValue());
}
}
return doDelete(builder.build().toString());
}catch (Exception e){
e.printStackTrace();
}
return null;
}

public static String doUploadFile(String url,MultipartFile[] uploadPics) throws IOException {
HttpResponse response = null;
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

for (MultipartFile uploadPic : uploadPics){
String fileName = uploadPic.getOriginalFilename();
builder.addBinaryBody("upfile[]", uploadPic.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
}
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
return EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} finally {
httpPost.releaseConnection();
}
return null;
}


public static void main(String[] args) {
// Map<String, String> paramsMap = new HashMap<String, String>();
// paramsMap.put("community_id", "13525");
// String result = getWithParams("http://api.5i5j.com.cn/v1/communityassess/isexsit", paramsMap);
// System.out.println(result);

Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("uid", "439");
paramsMap.put("type", "2");
paramsMap.put("houseid", "37115758");
String result = HttpUtil.doDelete("http://10.10.137.171/rv/RealTimeVisit/HouseDel", paramsMap);
System.out.println(result);
}

/**
* MultipartFile 转换成File
*
* @param multfile 原文件类型
* @return File
* @throws IOException
*/
public static File multipartToFile(MultipartFile multfile) throws IOException {
CommonsMultipartFile cf = (CommonsMultipartFile)multfile;
//这个myfile是MultipartFile的
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File file = fi.getStoreLocation();
//手动创建临时文件
// if(file.length() < 2048){
// File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +
// file.getName());
// multfile.transferTo(tmpFile);
// return tmpFile;
// }
return file;
}
}
原创粉丝点击