package com.*.*.*.util;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
public class HttpUtils {
/* @Autowired
private SysConfigMapper sysConfigMapper;*/
private static Logger logger = Logger.getLogger(HttpUtils.class);
static String web_url ="http://192.168.100.53:8080/*/*/" ;
static{
//初始化url参数
new HttpUtils().poolWebServiceUrl();
}
void poolWebServiceUrl() {
/* SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
SysConfig jpushConf = sysConfigMapper.selectByConfigkey("DEAL_ORGAN_PATH");
web_url=jpushConf.getConfigvalue();*/
}
/**
* @param url 请求地址
* @param jsonObject 请求的json数据
* @return
*/
public static String jsonPost(String url, JSONObject jsonObject) {
url=web_url+url+".html";
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String result = null;
try {
post.addHeader("Content-type","application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
post.setEntity(new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")));
HttpResponse res = httpclient.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(res.getEntity());// 返回json格式:
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
/**
* @param url 请求地址
* @param jsonObject 请求的json数据
* @return
*/
public static String jsonPost(String url, String args) {
url=web_url+url+".html";
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String result = null;
try {
post.addHeader("Content-type","application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
post.setEntity(new StringEntity(args, Charset.forName("UTF-8")));
HttpResponse res = httpclient.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(res.getEntity());// 返回json格式:
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
/**
* get请求
* @return
*/
public static String doGet(String url) {
CloseableHttpClient client = HttpClientBuilder.create().build();
try {
//发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/**请求发送成功,并得到响应**/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/**读取服务器返回过来的json字符串数据**/
String strResult = EntityUtils.toString(response.getEntity());
return strResult;
}
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
}