return JSONObject.fromObject(json);
}
return null;
}
/**
-
发起http get请求
-
@param url
-
@param params 请求的参数
-
*/
public String sendGet(String url, Map<String, String> params) {
Set keys = params.keySet();
StringBuilder urlBuilder = new StringBuilder(url + “?”);
for (String key : keys) {
urlBuilder.append(key).append(“=”).append(params.get(key)).append(“&”);
}
urlBuilder.delete(urlBuilder.length() - 1, urlBuilder.length());
return this.sendGet(urlBuilder.toString());
}
/**
-
发起post请求
-
@param url
-
@param params
-
*/
public String sendPost(String url, Map<String, String> params) {
return sendPost(url, params,null);
}
/**
-
发起post请求返回字符串
-
@param url
-
@param params 请求的参数
-
@param headers 请求的headers
-
*/
public String sendPost(String url, Map<String, String> params,Map<String,String> headers) {
String html = null;
HttpPost post = new HttpPost(url);
buildHeader(post, headers);
Integer httpResponseCode = 200;
Integer requestStatus = 0;
String result = “”;
try {
// 创建表单参数列表
List qparams = new ArrayList();
if (params != null) {
Set keys = params.keySet();
for (String key : keys) {
qparams.add(new BasicNameValuePair(key, params.get(key)));
}
// 设置编码
post.setEntity(new UrlEncodedFormEntity(qparams, this.charst));
}
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
logger.debug("Response content length: " + entity.getContentLength());
}
html = EntityUtils.toString(entity, this.charst);
result = html;
} catch (Exception e) {
requestStatus = 1;
result = ExceptionUtils.getStackTrace(e);
logger.error(“sendPost异常: url=” + url, e);
return null;
}
return html;
}
/**
-
发起post请求
-
@param url
-
@param params 请求的参数
-
*/
public JSONObject sendPostByJSON(String url, Map<String, String> params){
String json = sendPost(url, params);
if(StringUtils.isNotBlank(json)){
return JSONObject.fromObject(json);
}
return null;
}
/**
-
发起post请求
-
@param url
-
@param params 请求的参数
-
@param header 请求的header参数
-
*/
public JSONObject sendPostByJSON(String url, Map<String, String> params,Map<String,String> header){
String json = sendPost(url, params,header);
if(StringUtils.isNotBlank(json)){
return JSONObject.fromObject(json);
}
return null;
}
/**
-
post方法发送json数据
-
@param url
-
@param json
*/
public String sendPostJSON(String url, String json) {
return sendPostJSON(url, json, null);
}
/**
-
post方法发送json数据
-
@param url
-
@param json
*/
public JSONObject sendPostJSONByObj(String url, String json) {
String jsonTmp = sendPostJSON(url, json, null);
return JSONObject.fromObject(jsonTmp);
}
/**
-
post方法发送json数据
-
@param url
-
@param json
-
@param header 发送的header
*/
public String sendPostJSON(String url, String json, Map<String,String> header) {
HttpPost post = new HttpPost(url);
buildHeader(post,header);
Integer httpResponseCode = 200;
Integer requestStatus = 0;
String result = null;
try {
StringEntity stringentity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);// 解决中文乱码问题
post.setEntity(stringentity);
HttpResponse response = httpclient.execute(post);
httpResponseCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null) {
logger.debug("Response content length: " + entity.getContentLength());
}
result = EntityUtils.toString(entity, this.charst);
} catch (Exception e) {
requestStatus = 1;
result = ExceptionUtils.getStackTrace(e);
logger.error(“sendPostJson异常:”, e);
return null;
}
return result;
}
/**
- put方法发送json数据
*/
public String sendPutByJSON(String url, String json) {
HttpPut httpPut = new HttpPut(url);
Integer httpResponseCode = 200;
Integer requestStatus = 0;
String result = null;
Map<String,String> params = new HashMap<String,String>();
params.put(“json”,json);
try {
StringEntity stringentity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);// 解决中文乱码问题
httpPut.setEntity(stringentity);
HttpResponse response = httpclient.execute(httpPut);
HttpEntity entity = response.getEntity();
if (entity != null) {
logger.debug("Response content length: " + entity.getContentLength());
}
result = EntityUtils.toString(entity, this.charst);
} catch (Exception e) {
requestStatus = 1;
result = ExceptionUtils.getStackTrace(e);
logger.error(“sendPutByJson异常:”, e);
return null;
}
return result;
}
/**
- delete方法发送json数据
*/
public String sendDeleteByUrl(String url) {
Integer httpResponseCode = 200;
Integer requestStatus = 0;
String result = null;
HttpDelete httpDelete = new HttpDelete(url);
try {
HttpResponse response = httpclient.execute(httpDelete);
HttpEntity entity = response.getEntity();
if (entity != null) {
logger.debug("Response content length: " + entity.getContentLength());
}
result = EntityUtils.toString(entity, this.charst) 需要zi料+ 绿色徽【vip1024b】
;
} catch (Exception e) {
requestStatus = 1;
result = ExceptionUtils.getStackTrace(e);
logger.error(“sendDeleteByJson异常:”, e);
return null;
}
return result;
}
/**
-
获取请求http数据流,比如下载图片
-
*/
public InputStream sendGetInputStream(String url) {
if (url.startsWith(“https”)) {
httpclient = SSLClient.createSSLClientDefault();
}
RequestConfig.Builder builder = RequestConfig.custom();
builder.setCircularRedirectsAllowed(true);// 设置是否区分调转
RequestConfig config = builder.build();
HttpRequestBase httpGet = new HttpGet(url);
httpGet.setConfig(config);
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
return entity.getContent();
}
} catch (Exception e) {
e.printStackTrace();
logger.error(“获取http字节流失败 url=” + url, e);
return null;
}
return null;
}
/**
-
获取请求http数据流,比如下载图片
-
@param url
-
@param json
-
*/
public InputStream sendPostInputStream(String url, String json) {
HttpPost post = new HttpPost(url);
try {
StringEntity stringentity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);// 解决中文乱码问题
post.setEntity(stringentity);
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
InputStream iStream = entity.getContent();
Charset charset = ContentType.get(entity).getCharset();
if (entity.getContentType().getValue().startsWith(“application/json”)) {
int i = (int) entity.getContentLength();
if (i < 0) {
i = 4096;
}
final Reader reader = new InputStreamReader(iStream, charset);
final CharArrayBuffer buffer = new CharArrayBuffer(i);
final char[] tmp = new char[1024];
int l;
while ((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
}
return iStream;
} catch (Exception e) {
e.printStackTrace();
logger.error(“获取http字节流失败 url=” + url, e);
return null;
} finally {
try {
post.abort();
httpclient.close();
} catch (Exception e2) {
logger.error(“httpclient关闭异常>: url=” + url, e2);
}
}
}
/**
*进行post请求
-
@param content
-
@param url
-
*/
public String sendPost(String url, String content) {
HttpPost post = new HttpPost(url);
Integer httpResponseCode = 200;
Integer requestStatus = 0;
String result = null;
try {
StringEntity stringentity = new StringEntity(content.toString(), ContentType.APPLICATION_JSON);// 解决中文乱码问题
post.setEntity(stringentity);
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
logger.debug("Response content length: " + entity.getContentLength());
}
return EntityUtils.toString(entity, this.charst);
} catch (Exception e) {
requestStatus = 1;
result = ExceptionUtils.getStackTrace(e);
e.printStackTrace();
logger.error(“获取http字节流失败 url=” + url, e);
return null;
}
}
/**
-
发送http请求带有headers
-
@param url 请求的url
-
@param params 请求的数据
-
@param headers 请求的header
*/
public String sendGetHeaders(String url, Map<String, String> params, Map<String, String> headers) {
if(params != null && params.size() > 0){
Set keys = params.keySet();
StringBuilder urlBuilder = new StringBuilder(url + “?”);
for (String key : keys) {
urlBuilder.append(key).append(“=”).append(params.get(key)).append(“&”);
}
url = urlBuilder.delete(urlBuilder.length() - 1, urlBuilder.length()).toString();
}
String html = null;
RequestConfig.Builder builder = RequestConfig.custom();
builder.setCircularRedirectsAllowed(true);// 设置是否区分调转
RequestConfig config = builder.build();
HttpGet httpget = new HttpGet(url);
httpget.setConfig(config);
try {
for (Map.Entry<String, String> e : headers.entrySet()) {
httpget.addHeader(e.getKey(), e.getValue());
}
CloseableHttpResponse response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, this.charst);
} else {
logger.error(“http请求失败=” + response.getStatusLine().getStatusCode() + “url=” + url);
}
} catch (Exception e) {
logger.error(“http get请求异常”, e);
return null;
} finally {
httpget.abort();
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return html;
}
/**
- 用于上传微信素材
最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
);
}
CloseableHttpResponse response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, this.charst);
} else {
logger.error(“http请求失败=” + response.getStatusLine().getStatusCode() + “url=” + url);
}
} catch (Exception e) {
logger.error(“http get请求异常”, e);
return null;
} finally {
httpget.abort();
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return html;
}
/**
- 用于上传微信素材
最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
[外链图片转存中…(img-F6tl0FxH-1710369851650)]