我现在正在研究java web服务客户端。但是我面临一个很大的问题。我的问题是: 我通过其Web服务客户端生成器使用Eclipse生成Web服务客户端。它通过Apache Axis2生成Web服务。我生成了我的Web服务java代码,并使用wsdl成功使用它,不需要安全头。但我不能将它用于需要安全标头的Web服务。
我尝试用fiddler2获取我的请求。但是小提琴手没有捕获任何与我的请求相关的数据包。但是当我打印我创建的请求并使用带有soapui的xml时,操作成功执行。
增加超时设置不起作用。
我该怎么办?
我的xml和java错误如下:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
<wsse:ReplyTo xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Address>http://www.w3.org/2005/08/addressing/anonymous</wsse:Address>
</wsse:ReplyTo>
<wsse:MessageID xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">urn:uuid:123</wsse:MessageID>
<wsse:Action xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1">http://www.saglik.gov.tr/KPS/2011/KPSServices/TcKimlikNoIleKisiSorgula</wsse:Action>
</soapenv:Header>
<soapenv:Body>
<ns1:TcKimlikNoIleKisiSorgula xmlns:ns1="http://www.saglik.gov.tr/KPS/2011">
<ns1:tcNo>155***58</ns1:tcNo>
</ns1:TcKimlikNoIleKisiSorgula>
</soapenv:Body>
</soapenv:Envelope>
和错误消息:
[INFO] Unable to sendViaPost to url[https://kps.saglik.gov.tr/services/kpsservices.svc]
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
at sun.security.ssl.InputRecord.read(InputRecord.java:480)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.readLine(MultiThreadedHttpConnectionManager.java:1413)
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1098)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.axis2.transport.http.AbstractHTTPSender.executeMethod(AbstractHTTPSender.java:621)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:193)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)
at org.apache.axis2.description.OutInAxisOperationClient$NonBlockingInvocationWorker.run(OutInAxisOperation.java:446)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
我的代码在
之下public static void main(String[] args){
ServiceStub stub = new ServiceStub();
ServiceClient client = stub._getServiceClient();
client.addHeader(createSecurityHeader("username", "password"));
OMNamespaceImpl wsseNS = new OMNamespaceImpl(WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
OMFactory factory = new SOAP11Factory();
OMElementImpl replyToElement;
OMElementImpl addressElement;
OMElementImpl messageIDElement; OMElementImpl actionElement;
replyToElement = new OMElementImpl("ReplyTo", wsseNS, factory);
addressElement = new OMElementImpl("Address", wsseNS, replyToElement, factory);
addressElement.setText("http://www.w3.org/2005/08/addressing/anonymous");
client.addHeader(replyToElement);
messageIDElement = new OMElementImpl("MessageID", wsseNS, factory);
messageIDElement.setText("urn:uuid:"+123);
client.addHeader(messageIDElement);
actionElement = new OMElementImpl("Action", wsseNS, factory);
actionElement.addAttribute("mustUnderstand", "1", null);
actionElement.setText("http://www.saglik.gov.tr/KPS/2011/KPSServices/TcKimlikNoIleKisiSorgula");
client.addHeader(actionElement);
Options options = client.getOptions();
options.setProperty(HTTPConstants.CHUNKED, "false");
options.setProperty(HTTPConstants.HTTP_PROTOCOL_VERSION, HTTPConstants.HEADER_PROTOCOL_11);
TcKimlikNoIleKisiSorgula kisi = new TcKimlikNoIleKisiSorgula();
kisi.setTcNo(155*****58L);//require an long
stub.starttcKimlikNoIleKisiSorgula(kisi, callback);
}
public static OMElement createSecurityHeader(String username, String password)
{
OMNamespaceImpl wsseNS = new OMNamespaceImpl(WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
OMFactory factory = new SOAP11Factory();
OMElementImpl securityHeader;
OMElementImpl usernameTokenElement;
OMElementImpl usernameElement;
OMElementImpl passwordElement;
// create the Security header block
securityHeader = new OMElementImpl("Security", wsseNS, factory);
securityHeader.addAttribute("mustUnderstand", "1", null);
// nest the UsernameToken in the Security header
usernameTokenElement = new OMElementImpl(WSConstants.USERNAME_TOKEN_LN, wsseNS, securityHeader, factory);
// nest the Username and Password elements
usernameElement = new OMElementImpl(WSConstants.USERNAME_LN, wsseNS, usernameTokenElement, factory);
usernameElement.setText(username);
passwordElement = new OMElementImpl(WSConstants.PASSWORD_LN, wsseNS, usernameTokenElement, factory);
passwordElement.setText(password);
passwordElement.addAttribute(WSConstants.PASSWORD_TYPE_ATTR,
WSConstants.PASSWORD_TEXT, null);
return securityHeader;
}
答案 0 :(得分:1)
我遇到了同样的问题(我在wso2 esb环境中使用了axis2 web服务)。
我通过提供额外的超时配置来解决这个问题:
<parameter name="SO_TIMEOUT">some_integer_value</parameter>
<parameter name="CONNECTION_TIMEOUT">some_integer_value</parameter>
这也可以在Options
object:
options.setProperty(HTTPConstants.SO_TIMEOUT, new Integer(timeOutInMilliSeconds));
options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, new Integer(timeOutInMilliSeconds));
我使用值60000进行超时,因为我的Web服务预计将使用DB执行多项操作。