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就报错