简介:利用nginx 跳过证书,然后java代码调用nginx
工具类请求代码:
/**
* httpClient的get请求方式
*
* @return
* @throws Exception
*/
public String doGet(String url) throws Exception {
/* 1 生成 HttpClinet 对象并设置参数 */
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url.trim());
getMethod.setRequestHeader("Content-Type", "application/json");
getMethod.setRequestHeader("Host",“放”);
getMethod.setRequestHeader("Authorization", "Token "+bizConf.get("INDelhivery_Token").trim());
String response = "";
/* 2 执行 HTTP GET 请求 */
try {
int statusCode = httpClient.executeMethod(getMethod);
/* 3 判断访问的状态码 */
if (statusCode != HttpStatus.SC_OK) {
System.err.println("请求出错: " + getMethod.getStatusLine());
}
// 读取 HTTP 响应内容,这里简单打印网页内容
byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
response = new String(responseBody, "UTF-8");
} finally {
/* 4 .释放连接 */
getMethod.releaseConnection();
}
return response;
}
/**
* httpClient的post请求方式
*
* @return
* @throws Exception
*/
public String doPost(String jsonData, String url) throws Exception {
String postResult = "";
//第一步定义好httpClient
HttpClient httpClient = new HttpClient();
//第二步定义好postMethod
PostMethod postMethod = new PostMethod(url.trim());
//第三步定义头
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
postMethod.setRequestHeader("Host", bizConf.get("INDelhivery_Host"));
postMethod.setRequestHeader("Authorization", "Token "+bizConf.get("INDelhivery_Token").trim());
//第四步封装请求参数
postMethod.setParameter("format", "json");
postMethod.setParameter("data", jsonData);
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
logger.info("请求出错: " + postMethod.getStatusLine());
}
//第五步处理返回参数
postResult = IOUtils.toString(postMethod.getResponseBodyAsStream(), "UTF-8");
} finally {
//第六步 释放连接
postMethod.releaseConnection();
}
return postResult;
}
public String doPut(String jsonData, String url) throws Exception {
String postResult = "";
HttpClient httpClient = new HttpClient();
PutMethod putMethod = new PutMethod(url.trim());
putMethod.setRequestHeader("Content-Type", "application/json");
putMethod.setRequestHeader("Accept", "*/*");
putMethod.setRequestHeader("User-Agent", "Mozilla 5.0");
putMethod.setRequestHeader("Host", "digitalapi.auspost.com.au");
String accountNumber=bizConf.get("AUPost_accountNumber");
putMethod.setRequestHeader("account-number", accountNumber);
putMethod.setRequestHeader("Cache-Control", "no-cache");
// 获取加密结果并转BASE64
putMethod.setRequestHeader("Authorization", getHeader());
// 封装请求参数
StringRequestEntity requestEntity = new StringRequestEntity(jsonData, "application/json", "UTF-8");
putMethod.setRequestEntity(requestEntity);
try {
StringBuffer sb = new StringBuffer();
int statusCode = httpClient.executeMethod(putMethod);
InputStream is = putMethod.getResponseBodyAsStream();
byte[] b = new byte[1024];
int len = 0;
while((len = is.read(b)) > 0) {
sb.append(new String(b, 0, len));
}
postResult = sb.toString();
} finally {
putMethod.releaseConnection();
}
return postResult;
}