我正在使用WSDL编写SOAP客户端。我在PHP中有类似的代码,效果很好。但是,我在Java中有一些问题。这是我的代码段:

我应该在哪里定义本地WSDL文件的路径?

任何想法都欢迎。

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;
}


运行此代码后,在行SOAPMessage soapResponse = soapConnection.call(mySampleQuery(),"https://www.webpagename.com")处出现以下错误:


  2014年11月3日下午4:18:27
  com.sun.xml.internal.messaging.saaj.soap.MessageImpl
  identityContentType严重:SAAJ0537:无效的内容类型。可能
  错误消息而不是SOAP消息
  向服务器发送SOAP请求
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效
  内容类型:文本/ html。这是一条错误消息,而不是SOAP吗?
  响应?在
  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:
  无效的Content-Type:文本/ html。这是错误消息,而不是
  SOAP响应?在
  com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(未知
  来源)
  com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(未知
  来源)
  com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(未知
  资源)

最佳答案

无效的内容类型。可能是错误消息,而不是SOAP消息
  


那是您的第一个线索。


  
    无效的Content-Type:text / html。这是错误消息,而不是SOAP响应
  


那是另一个线索。放在一起:


您的客户正在连接该服务
该服务正在返回一个非SOAP响应有效负载(它正在返回HTML),因此SAAJ对此感到窒息。


通常,Web服务在响应标准JavaEE容器生成的错误页面时会返回HTML,这与预期的SOAP响应相反。现在的问题是:返回的错误响应是什么?与您的服务提供商联系,以获取有关您做错了什么的反馈。另外,请考虑记录一切以某种方式影响客户的内容

有关


Tracing XML request/responses with JAX-WS when error occurs

09-09 18:28