简体   繁体   English

使用WSDL的SOAP客户端

[英]SOAP client using WSDL

I am writing SOAP client using WSDL. 我正在使用WSDL编写SOAP客户端。 I have similar code in PHP, which works fine. 我在PHP中有类似的代码,工作正常。 However, I have some problems in Java. 但是,我在Java中遇到了一些问题。 Here is my code snippet: 这是我的代码片段:

Where should I define the path to my local WSDL file? 我应该在哪里定义本地WSDL文件的路径?

Any idea is welcome. 欢迎任何想法。

try 
{
 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
 SOAPConnection soapConnection = soapConnectionFactory.createConnection();

 SOAPMessage soapResponse = soapConnection.call(mySampleQuery(), 
                     "https://www.webpagename.com");

 // Process the SOAP Response
 printSOAPResponse(soapResponse);

 soapConnection.close();
} 
catch (Exception e) 
{
 System.err.println("Error occurred while sending SOAP Request to Server");
 e.printStackTrace();
}

private static SOAPMessage queryFlightsByAerodrome() throws Exception
{     
 MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
 SOAPMessage soapMessage = messageFactory.createMessage();
 SOAPPart soapPart = soapMessage.getSOAPPart();

 String serverURI = "localfolder/wsdlfilename.wsdl";

 // SOAP Envelope
 SOAPEnvelope envelope = soapPart.getEnvelope();
 envelope.addNamespaceDeclaration("mydata", serverURI);

 // SOAP Body
 SOAPBody soapBody = envelope.getBody();
 SOAPElement soapBodyElem = soapBody.addChildElement("mySampleRequest", "mydata");

 SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("userId");
 soapBodyElem1.addTextNode("testuser");

 //...here I create soapBodyElem1 

 MimeHeaders headers = soapMessage.getMimeHeaders();

 headers.addHeader("SOAPAction", serverURI + "queryTestData");

 System.out.println("Content description: "+soapMessage.getContentDescription());
 soapMessage.saveChanges();

 /* Print the request message */
 System.out.print("Request SOAP Message:");
 soapMessage.writeTo(System.out);
 System.out.println();

 return soapMessage;
}

After running this code I get the following error at line SOAPMessage soapResponse = soapConnection.call(mySampleQuery(),"https://www.webpagename.com") : 运行此代码后,我在SOAPMessage soapResponse = soapConnection.call(mySampleQuery(),"https://www.webpagename.com")行收到以下错误:

Nov 03, 2014 4:18:27 PM com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType SEVERE: SAAJ0537: Invalid Content-Type. 2014年11月3日下午4:18:27 com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType SEVERE:SAAJ0537:内容类型无效。 Could be an error message instead of a SOAP message Error occurred while sending SOAP Request to Server com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. 可能是错误消息而不是SOAP消息将SOAP请求发送到服务器时发生错误com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效的内容类型:text / html的。 Is this an error message instead of a SOAP response? 这是一条错误消息而不是SOAP响应吗? at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(Unknown Source) at com.aslogic.isagent.MyAgent.main(MyAgent.java:46) Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. 在com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(未知来源)com.aslogic.isagent.MyAgent.main(MyAgent.java:46)引起:com.sun.xml。 internal.messaging.saaj.SOAPExceptionImpl:无效的内容类型:text / html。 Is this an error message instead of a SOAP response? 这是一条错误消息而不是SOAP响应吗? at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(Unknown Source) at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(Unknown Source) at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(Unknown Source) com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(Unknown Source)at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(Unknown Source)at com.sun.xml .internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(未知来源)

Invalid Content-Type. 内容类型无效。 Could be an error message instead of a SOAP message 可能是错误消息而不是SOAP消息

That's your first clue right there. 那是你的第一个线索。

Invalid Content-Type:text/html. 内容类型无效:text / html。 Is this an error message instead of a SOAP response 这是一条错误消息而不是SOAP响应

That's another clue. 那是另一个线索。 Putting it all together: 把它们放在一起:

  1. Your client is connecting to the service 您的客户正在连接该服务
  2. The service is returning a non-SOAP response payload (it's returning HTML instead), hence the reason why SAAJ is choking on it. 该服务返回一个非SOAP响应有效负载(它返回HTML),因此SAAJ窒息它的原因。

Typically, a webservice will return HTML when it's responding with a standard JavaEE container-generated error page, as against the expected SOAP response. 通常,Web服务在使用标准JavaEE容器生成的错误页面进行响应时将返回HTML,与预期的SOAP响应相反。 The question now is: What is the error response that's being returned? 现在的问题是: 返回的错误响应什么? Talk to your service provider for feedback on what you're doing wrong. 与您的服务提供商联系,获取有关您做错的反馈。 Also, look into logging everything that hits your client somehow 另外,请记录以某种方式记录击中客户端的所有内容

Related 有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM