基于Https的Java-soapweb服务
我对使用https的SOAP非常陌生。我的一个项目需要进行SOAP调用才能通过https访问web服务 服务提供商只给了我们三个文件.jks、.p12。还有一个代码示例。txt文件 我的第一个问题是,在没有WSDL文件的情况下调用Webservice是可能的吗 在服务提供商给我的示例代码的帮助下,我编写了以下客户机代码 但我收到了以下错误消息:java.net.SocketException:Socket未连接:connect基于Https的Java-soapweb服务,java,web-services,soap,https,soapui,Java,Web Services,Soap,Https,Soapui,我对使用https的SOAP非常陌生。我的一个项目需要进行SOAP调用才能通过https访问web服务 服务提供商只给了我们三个文件.jks、.p12。还有一个代码示例。txt文件 我的第一个问题是,在没有WSDL文件的情况下调用Webservice是可能的吗 在服务提供商给我的示例代码的帮助下,我编写了以下客户机代码 但我收到了以下错误消息:java.net.SocketException:Socket未连接:connect package javaapplication7; impor
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 + "\n" + "<soapenv:Header/><soapenv:Body>";
xmlString = xmlString + "\n" + "<initiatePaymentDetailsReq>";
xmlString = xmlString + "\n" + "<olpIdAlias>xxxx</olpIdAlias>";
xmlString = xmlString + "\n" + "<merchantId>xxxx</merchantId>";
xmlString = xmlString + "\n" + "<merchantRefNum>2016081870001</merchantRefNum>";
xmlString = xmlString + "\n" + "<paymentAmount>100</paymentAmount>";
xmlString = xmlString + "\n" + "<paymentCurrency>SAR</paymentCurrency>";
xmlString = xmlString + "\n" + "<dynamicMerchantLandingURL>http://sweetroomksa.com/index.php?route=payment/custom/confirm</dynamicMerchantLandingURL>";
xmlString = xmlString + "\n" + "<dynamicMerchantFailureURL>#</dynamicMerchantFailureURL>";
xmlString = xmlString + "\n" + "</initiatePaymentDetailsReq>";
xmlString = xmlString + "\n" + "</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();
}
}
}
包javaapplication7;
导入java.io.FileInputStream;
导入java.io.InputStreamReader;
导入java.io.OutputStreamWriter;
导入java.net.URL;
导入java.security.KeyStore;
导入java.security.KeyStore.SecretKeyEntry;
导入javax.crypto.SecretKey;
导入javax.net.ssl.HostnameVerifier;
导入javax.net.ssl.HttpsURLConnection;
导入javax.net.ssl.SSLSession;
/**
*
*@作者dhanish
*/
公共类JavaApplication7{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
尝试
{
String keyPath=“C:\\key.jks”;
字符串keyPass=“xxx”;
字符串keyType=“JKS”;
//SSL密钥库的路径
setProperty(“javax.net.ssl.keyStore”,keyPath);
setProperty(“javax.net.ssl.keystrepassword”,keyPass);
setProperty(“javax.net.ssl.keyStoreType”,keyType);
//构建要发布的XML
字符串xmlString=“”;
xmlString=xmlString+“\n”+”;
xmlString=xmlString+“\n”+”;
xmlString=xmlString+“\n”+“xxxx”;
xmlString=xmlString+“\n”+“xxxx”;
xmlString=xmlString+“\n”+“2016081870001”;
xmlString=xmlString+“\n”+“100”;
xmlString=xmlString+“\n”+“SAR”;
xmlString=xmlString+“\n”+”http://sweetroomksa.com/index.php?route=payment/custom/confirm";
xmlString=xmlString+“\n”+“#”;
xmlString=xmlString+“\n”+”;
xmlString=xmlString+“\n”+”;
//通过HTTPS发布XML
URL=新URL(“https://xxx.xxx.com/soap?service=RB_OLP_INITIATE_PAYMENT“”;//替换
HttpsURLConnection连接=(HttpsURLConnection)url.openConnection();
connection.setRequestMethod(“POST”);
connection.setDoOutput(真);
setRequestProperty(“内容类型”、“文本/xml”);
connection.setHostnameVerifier(新的HostnameVerifier()
{
公共布尔验证(字符串主机名、SSLSession会话)
{
返回true;
}
});
connection.connect();
//告诉web服务器我们正在发送什么
OutputStreamWriter writer=新的OutputStreamWriter(connection.getOutputStream());
write.write(xmlString);
writer.flush();
writer.close();
//阅读回应
InputStreamReader=新的InputStreamReader(connection.getInputStream());
StringBuilder buf=新的StringBuilder();
char[]cbuf=新字符[2048];
int-num;
而(-1!=(num=reader.read(cbuf)))
{
追加(cbuf,0,num);
}
字符串结果=buf.toString();
系统输出打印项次(结果);
}
捕获(例外e)
{
System.out.println(e.getCause());
e、 printStackTrace();
}
}
}
输出
null
java.net.SocketException:未连接套接字:连接
位于java.net.DualStackPlainSocketImpl.connect0(本机方法)
位于java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
位于java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
位于java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
位于java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
位于java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
位于java.net.socksocketimpl.connect(socksocketimpl.java:392)
位于java.net.Socket.connect(Socket.java:589)
位于sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:656)
位于sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
位于sun.net.NetworkClient.doConnect(NetworkClient.java:180)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
在sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:275)
位于sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
位于sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
在javaapplication7.javaapplication7.main(javaapplication7.java:78)
有人能帮我找出错误是什么吗?我找到了错误并解决了它。此错误的原因是服务器防火墙上的SSL端口(默认值为443)未打开。所以我联系了服务公司。我把我的静态公共IP给了他们。给了我访问权限是的,只要知道端点URL,就可以在没有wsdl的情况下调用web服务。。就像你在做普通的httpsURLConnection或者saajsoap框架等等。。尝试连接SOAP UI工具,如果您有..您添加了certifi吗
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)