Java- SOAP Web service over Https
我对带有 https 的 SOAP 非常陌生。我的一个项目需要进行 SOAP 调用才能通过 HTTPS 访问 Web 服务。
服务商只给了我们三个文件.jks, .p12。和一个代码示例。 .txt 文件
我的第一个问题是可以在没有 WSDL 文件的情况下调用 Webservice。?
我在服务提供商给我的示例代码的帮助下编写了以下客户端代码。
但我收到此错误消息:java.net.SocketException: Socket is not connected: connect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | package javaapplication7; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.security.KeyStore; import java.security.KeyStore.SecretKeyEntry; import javax.crypto.SecretKey; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; /** * * @author dhanish */ public class JavaApplication7 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here try { String keyPath ="C:\\\\key.jks"; String keyPass ="xxx"; String keyType ="JKS"; //path to SSL keystore System.setProperty("javax.net.ssl.keyStore", keyPath); System.setProperty("javax.net.ssl.keyStorePassword", keyPass); System.setProperty("javax.net.ssl.keyStoreType", keyType); //build the XML to post String xmlString ="<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://olp.sadad.com/sadadpaymentmanagement/service/olppaymentmanager/req">"; xmlString = xmlString +"\ " +"<soapenv:Header/><soapenv:Body>"; xmlString = xmlString +"\ " +"<initiatePaymentDetailsReq>"; xmlString = xmlString +"\ " +"xxxx</olpIdAlias>"; xmlString = xmlString +"\ " +"<merchantId>xxxx</merchantId>"; xmlString = xmlString +"\ " +"<merchantRefNum>2016081870001</merchantRefNum>"; xmlString = xmlString +"\ " +"<paymentAmount>100</paymentAmount>"; xmlString = xmlString +"\ " +"<paymentCurrency>SAR</paymentCurrency>"; xmlString = xmlString +"\ " +"<dynamicMerchantLandingURL>http://sweetroomksa.com/index.php?route=payment/custom/confirm</dynamicMerchantLandingURL>"; xmlString = xmlString +"\ " +"<dynamicMerchantFailureURL>#</dynamicMerchantFailureURL>"; xmlString = xmlString +"\ " +"</initiatePaymentDetailsReq>"; xmlString = xmlString +"\ " +"</soapenv:Body></soapenv:Envelope>"; //post XML over HTTPS URL url = new URL("https://xxx.xxx.com/soap?service=RB_OLP_INITIATE_PAYMENT"); //replace HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput( true ); connection.setRequestProperty("Content-Type","text/xml" ); connection.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); connection.connect(); //tell the web server what we are sending OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(xmlString); writer.flush(); writer.close(); // reading the response InputStreamReader reader = new InputStreamReader(connection.getInputStream()); StringBuilder buf = new StringBuilder(); char[] cbuf = new char[ 2048 ]; int num; while ( -1 != (num=reader.read( cbuf ))) { buf.append( cbuf, 0, num ); } String result = buf.toString(); System.out.println(result); } catch(Exception e) { System.out.println(e.getCause()); e.printStackTrace(); } } } |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | null java.net.SocketException: Socket is not connected: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:656) at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173) at sun.net.NetworkClient.doConnect(NetworkClient.java:180) at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275) at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153) at javaapplication7.JavaApplication7.main(JavaApplication7.java:78) |
谁能帮我找出错误是什么。?
我找到了错误所在并解决了它。此错误的原因 SSL 端口(默认为 443)未在服务器的防火墙上打开。所以我联系了服务公司。我把我的静态公共 IP 给了他们。给了我访问权限