java1.7后台请求https接口问题记录

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值