mirror of
https://gitee.com/dromara/MaxKey.git
synced 2025-12-08 01:48:33 +08:00
HttpRequestAdapter optimize
This commit is contained in:
parent
4ccf108944
commit
3e9f0bf502
@ -28,6 +28,7 @@ import java.util.Set;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
|
import org.apache.http.ParseException;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
@ -46,6 +47,7 @@ import org.maxkey.util.AuthorizationHeaderUtils;
|
|||||||
import org.maxkey.util.JsonUtils;
|
import org.maxkey.util.JsonUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ -113,20 +115,14 @@ public class HttpRequestAdapter {
|
|||||||
stringEntity.setContentType(ContentType.APPLICATION_JSON);
|
stringEntity.setContentType(ContentType.APPLICATION_JSON);
|
||||||
httpMethod.setEntity(stringEntity);
|
httpMethod.setEntity(stringEntity);
|
||||||
}
|
}
|
||||||
_logger.debug("Post Message \n{} ", httpMethod.getEntity().toString());
|
_logger.trace("Post Message \n{} ", httpMethod.getEntity().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// httpClient对象执行post请求,并返回响应参数对象
|
// httpClient对象执行post请求,并返回响应参数对象
|
||||||
httpResponse = httpClient.execute(httpMethod);
|
httpResponse = httpClient.execute(httpMethod);
|
||||||
// 从响应对象中获取响应内容
|
// 从响应对象中获取响应内容
|
||||||
HttpEntity entity = httpResponse.getEntity();
|
return resolveHttpResponse(httpResponse);
|
||||||
String content = EntityUtils.toString(entity);
|
|
||||||
_logger.debug("Http Response StatusCode {} , Content {}",
|
|
||||||
httpResponse.getStatusLine().getStatusCode(),
|
|
||||||
content
|
|
||||||
);
|
|
||||||
return content;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -158,13 +154,7 @@ public class HttpRequestAdapter {
|
|||||||
// httpClient对象执行put请求,并返回响应参数对象
|
// httpClient对象执行put请求,并返回响应参数对象
|
||||||
httpResponse = httpClient.execute(httpMethod);
|
httpResponse = httpClient.execute(httpMethod);
|
||||||
// 从响应对象中获取响应内容
|
// 从响应对象中获取响应内容
|
||||||
HttpEntity entity = httpResponse.getEntity();
|
return resolveHttpResponse(httpResponse);
|
||||||
String content = EntityUtils.toString(entity);
|
|
||||||
_logger.debug("Http Response StatusCode {} , Content {}",
|
|
||||||
httpResponse.getStatusLine().getStatusCode(),
|
|
||||||
content
|
|
||||||
);
|
|
||||||
return content;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -189,19 +179,13 @@ public class HttpRequestAdapter {
|
|||||||
StringEntity stringEntity =new StringEntity(jsonString, "UTF-8");
|
StringEntity stringEntity =new StringEntity(jsonString, "UTF-8");
|
||||||
stringEntity.setContentType(ContentType.APPLICATION_JSON);
|
stringEntity.setContentType(ContentType.APPLICATION_JSON);
|
||||||
httpMethod.setEntity(stringEntity);
|
httpMethod.setEntity(stringEntity);
|
||||||
_logger.debug("Post Message \n{} ", httpMethod.getEntity().toString());
|
_logger.debug("Put Message \n{} ", httpMethod.getEntity().toString());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// httpClient对象执行put请求,并返回响应参数对象
|
// httpClient对象执行put请求,并返回响应参数对象
|
||||||
httpResponse = httpClient.execute(httpMethod);
|
httpResponse = httpClient.execute(httpMethod);
|
||||||
// 从响应对象中获取响应内容
|
// 从响应对象中获取响应内容
|
||||||
HttpEntity entity = httpResponse.getEntity();
|
return resolveHttpResponse(httpResponse);
|
||||||
String content = EntityUtils.toString(entity);
|
|
||||||
_logger.debug("Http Response StatusCode {} , Content {}",
|
|
||||||
httpResponse.getStatusLine().getStatusCode(),
|
|
||||||
content
|
|
||||||
);
|
|
||||||
return content;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -231,13 +215,7 @@ public class HttpRequestAdapter {
|
|||||||
// httpClient对象执行get请求,并返回响应参数对象
|
// httpClient对象执行get请求,并返回响应参数对象
|
||||||
httpResponse = httpClient.execute(httpMethod);
|
httpResponse = httpClient.execute(httpMethod);
|
||||||
// 从响应对象中获取响应内容
|
// 从响应对象中获取响应内容
|
||||||
HttpEntity entity = httpResponse.getEntity();
|
return resolveHttpResponse(httpResponse);
|
||||||
String content = EntityUtils.toString(entity);
|
|
||||||
_logger.debug("Http Response StatusCode {} , Content {}",
|
|
||||||
httpResponse.getStatusLine().getStatusCode(),
|
|
||||||
content
|
|
||||||
);
|
|
||||||
return content;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -261,13 +239,7 @@ public class HttpRequestAdapter {
|
|||||||
// httpClient对象执行post请求,并返回响应参数对象
|
// httpClient对象执行post请求,并返回响应参数对象
|
||||||
httpResponse = httpClient.execute(httpMethod);
|
httpResponse = httpClient.execute(httpMethod);
|
||||||
// 从响应对象中获取响应内容
|
// 从响应对象中获取响应内容
|
||||||
HttpEntity entity = httpResponse.getEntity();
|
return resolveHttpResponse(httpResponse);
|
||||||
String content = EntityUtils.toString(entity);
|
|
||||||
_logger.debug("Http Response StatusCode {} , Content {}",
|
|
||||||
httpResponse.getStatusLine().getStatusCode(),
|
|
||||||
content
|
|
||||||
);
|
|
||||||
return content;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -276,6 +248,15 @@ public class HttpRequestAdapter {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String resolveHttpResponse(CloseableHttpResponse httpResponse) throws ParseException, IOException {
|
||||||
|
HttpEntity entity = httpResponse.getEntity();
|
||||||
|
String content = EntityUtils.toString(entity);
|
||||||
|
HttpStatus httpStatus = HttpStatus.valueOf(httpResponse.getStatusLine().getStatusCode());
|
||||||
|
_logger.debug("Http Response HttpStatus {} " , httpStatus);
|
||||||
|
_logger.trace("Http Response Content {} " , content );
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param HttpRequest
|
* @param HttpRequest
|
||||||
* @param headers
|
* @param headers
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user