JAVA SOAP服务器故障

时间:2016-02-11 07:14:23

标签: xml web-services soap wsdl

发送SOAP请求时出现此错误。请帮我解决我的错误。

发送SOAP请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><GetCitiesByCountry xmlns="http://www.webserviceX.NET/"><CountryName>India</CountryName></GetCitiesByCountry></SOAP-ENV:Body></SOAP-ENV:Envelope>
Received SOAP reply:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Data.SqlClient.SqlException: Procedure or function 'getWCity' expects parameter '@CountryName', which was not supplied.
   at WebServicex.GlobalWeather.GetCitiesByCountry(String CountryName)
   --- End of inner exception stack trace ---</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientwithoutwsdl;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;

public class ClientWithoutWSDL {

    private final static String SERVICE_HOST = "http://www.webserviceX.NET/";
    private final static String SERVICE_METHOD = "GetCitiesByCountry";
    private final static String SERVICE_ENDPOINT = "globalweather.asmx";

    private final static String country = "India";

    public static void main(String args[]) {
     try {
         // Create a new SOAP connection
         SOAPConnectionFactory soapConnectionFactory =
         SOAPConnectionFactory.newInstance();
         SOAPConnection soapConnection = soapConnectionFactory.createConnection();

         // Send a SOAP Message to SOAP server
         // Send this message to http://www.webserviceX.NET/stockquote.asmx
         SOAPMessage soapResponse = soapConnection.call(
            createSOAPRequest(country),
            SERVICE_HOST + SERVICE_ENDPOINT);

         System.out.println("Received SOAP reply:");
         soapResponse.writeTo(System.out);
         System.out.println("\r\n");

         // Close the connection
         soapConnection.close();
      } catch (Exception e) {
         System.err.println("Fatal error occurred");
         e.printStackTrace();
      }
   }

    private static SOAPMessage createSOAPRequest(String stockSymbol) throws Exception {
        // Construct a new SOAP request message
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        // Construct the SOAP "body" with the method arguments
        SOAPBody soapBody = soapMessage.getSOAPBody();
        QName bodyName = new QName(SERVICE_HOST, SERVICE_METHOD);
        SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName);

        SOAPElement soapBodyArgument1 = bodyElement.addChildElement("CountryName");
        soapBodyArgument1.addTextNode(country); 

        // Add a SOAP action header to the request

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", SERVICE_HOST + SERVICE_METHOD);

        soapMessage.saveChanges();

        // Print out the request message:
        System.out.println("Sending SOAP request:");
        soapMessage.writeTo(System.out);
        System.out.printf("%n");
        return soapMessage;
    }
}

2 个答案:

答案 0 :(得分:1)

你打印XML

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header/>
    <env:Body>
        <GetCitiesByCountry xmlns="http://www.webserviceX.NET/">
            <CountryName xmlns="">India</CountryName>
        </GetCitiesByCountry>
    </env:Body>
</env:Envelope>

具有countryName的默认命名空间,但应为http://www.webserviceX.NET

http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry

答案 1 :(得分:0)

SOAP 1.1

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
  <GetCitiesByCountry xmlns="http://www.webserviceX.NET">
  <CountryName>string</CountryName>
  </GetCitiesByCountry>
  </soap:Body>
</soap:Envelope>

SOAP 1.2

<?xml version="1.0" encoding="utf-8"?>
  <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
  <GetCitiesByCountry xmlns="http://www.webserviceX.NET">
  <CountryName>string</CountryName>
  </GetCitiesByCountry>
  </soap12:Body>
</soap12:Envelope>

参考:http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry