日期:2014-05-17  浏览次数:20950 次

调用https在我本地是好的,放到服务器上就不行
代码如下
public static String DirectSendData(PinAnURL pinAnURL, String sInputStream) {
String sResult = "";
String sData = "";
/*
* Protocol myhttps = new Protocol("https", new MySSLSocketFactory(),
* 443); Protocol.registerProtocol("https", myhttps); HttpClient client
* = new HttpClient(); HttpMethod post = new PostMethod(url);
*/
try {
// System.out.println("pinAnURL.getSendUrl(): " +
// pinAnURL.getSendUrl());
URL url = new URL(pinAnURL.getSendUrl());
System
.setProperty("javax.net.ssl.keyStore", pinAnURL
.getKeyStore());

System.setProperty("javax.net.ssl.keyStorePassword", pinAnURL
.getKeyStorePassword());
System.setProperty("javax.net.ssl.keyStoreType", pinAnURL
.getKeyStoreType());
System.setProperty("javax.net.ssl.trustStorePassword", pinAnURL
.getTrustStorePassword());

System.setProperty("javax.net.ssl.trustStore", pinAnURL
.getTrustStore());

System.out.println("javax.net.ssl.keyStorePassword: "
+ System.getProperty("javax.net.ssl.keyStorePassword"));
System.out.println("javax.net.ssl.keyStoreType:"
+ System.getProperty("javax.net.ssl.keyStoreType"));
System.out.println("javax.net.ssl.trustStorePassword:"
+ System.getProperty("javax.net.ssl.trustStorePassword"));
System.out.println("KeyStore:"
+ System.getProperty("javax.net.ssl.keyStore"));
System.out.println("TrustStore:"
+ System.getProperty("javax.net.ssl.trustStore"));
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName
+ " vs. " + session.getPeerHost());
return true;
}
};

HttpsURLConnection.setDefaultHostnameVerifier(hv);

HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
connection.setRequestProperty("Content-Type", "text/xml");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(1000 * 20);
System.out.println("sInputStream:" + sInputStream);
byte data[] = sInputStream.getBytes("gbk");
try {
OutputStream out = connection.getOutputStream();
out.write(data);
out.flush();
out.close();
String aLine = "";
InputStream is = connection.getInputStream();

InputStreamReader inReader = new InputStreamReader(is, "UTF-8");
// InputStreamReader inReader = new InputStreamReader(is,
// "GBK");
BufferedReader aReader = new BufferedReader(inReader);
while ((aLine = aReader.readLine()) != null) {
// aLine = new String(aLine.getBytes("gbk"), "utf-8");
// System.out.println("aLine: " + new
// String(aLine.getBytes("utf-8"), "gbk"));
sData += aLine;
}
System.out.println("sData: " + sData);
// sData = new String(sData.getBytes("GBK"), "UTF-8");
sResult = sData;
inReader.close();
aReader.close();
connection.disconnect();
inReader = null;
aReader = null;

} catch (Exception e) {
e.printStackTrace();
sResult = "";