出色的本色 2018-06-05 08:05 采纳率: 0%
浏览 3647
已结题

HttpURLConnection请求https报Connection reset

private String getAllFolderInfo(String sid) {
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
StringBuffer result = new StringBuffer();
try {
URL url = new URL("https://XX.XX.XX.XX/coremail/s?func=mbox:getAllFolders&sid=" + sid);
if (url.getProtocol().toLowerCase().equals("https")) {
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
connection = https;
} else {
connection = (HttpURLConnection) url.openConnection();
}
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Content-Type", "application/xml");
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
// 获取URLConnection对象对应的输出流
DataOutputStream ds = null;
try
{
ds = new DataOutputStream(connection.getOutputStream());
String postBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>true";

        ds.write(postBody.toString().getBytes("UTF-8"));
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        ds.flush();
        ds.close();
      }
      bufferedReader = new BufferedReader(
        new InputStreamReader(connection.getInputStream(), "utf-8"));
      String temp = null;
      while ((temp = bufferedReader.readLine()) != null)
        result.append(temp);
    }
    catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (bufferedReader != null) {
        try {
          bufferedReader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      connection.disconnect();
    }
    System.out.println("result==="+result.toString());
    return result.toString();
  }
  private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    private static void trustAllHosts() {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[]{};
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            }
        }};
        // Install the all-trusting trust manager
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
使用  connection.connect();手动开启,或者ds = new DataOutputStream(connection.getOutputStream());自动开启都会报Connection reset。。。tomcat服务器下正常,weblogic就报错

图片说明

  • 写回答

3条回答 默认 最新

  • yanglei15237729022 2018-06-05 08:09
    关注

    链接上面出现了问题,看看网络怎么样

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog