java后台请求接口,测试没有问题,上正式出现报错,没截图,应该是connection reset的报错,发现测试环境请求接口地址是http,正式环境接口是https,在java1.7的环境下不能通用,需修改代码。做代码记录:
原请求代码:
/**
* POST
*/
public String doPost(String url) throws Exception{
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Post请求
HttpPost httpPost = new HttpPost(url);
// 响应模型
CloseableHttpResponse response = null;
String messtoken="";
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
messtoken=EntityUtils.toString(responseEntity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return messtoken;
}
/**
* GET
*/
public String doGet(String url) throws Exception{
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet(url);
// 响应模型
CloseableHttpResponse response = null;
String messtoken="";
try {
// 由客户端执行(发送)get请求
response = httpClient.execute(httpget);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
messtoken=EntityUtils.toString(responseEntity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return messtoken;
}
/**
* 带body参数的请求
*/
public static String sendPost(String serverURL, String params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
InputStream is = null;
OutputStreamWriter writer = null;
try{
StringBuffer sbf = new StringBuffer();
String strRead = null;
URL url = new URL(serverURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");//请求post方式
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json;charset=\"UTF-8\"");
connection.connect();
writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
//body参数放这里
writer.write(params);
writer.flush();
is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
is.close();
writer.close();
connection.disconnect();
String results = sbf.toString();
System.out.println("str_base>>>:"+results);
return results;
}catch (IOException e){
e.printStackTrace();
return "";
}finally {
try {
if(connection != null){
connection.disconnect();
}
if(reader != null){
reader.close();
}
if(is != null){
is.close();
}
if(writer != null){
writer.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
修改后代码:
//用于普通请求
private static SSLConnectionSocketFactory getSslConnectionSocketFactory() {
return new SSLConnectionSocketFactory(SSLContexts.createDefault()){
@Override
protected void prepareSocket(SSLSocket so