JAVA中的SOAP请求:无法添加包含SOAP命名空间中元素的片段

JAVA中的SOAP请求:无法添加包含SOAP命名空间中元素的片段,java,xml,soap,wsdl,soap-client,Java,Xml,Soap,Wsdl,Soap Client,我目前正在用Java编写一个独立的soap请求/响应应用程序,并使用以下项目: 我使用此示例WSDL来开发代码: 从上面的WSDL中,我试图调用操作:LatLonListCityNames 其中包含以下soap请求: <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http:/

我目前正在用Java编写一个独立的soap请求/响应应用程序,并使用以下项目:

我使用此示例WSDL来开发代码:

从上面的WSDL中,我试图调用操作:LatLonListCityNames 其中包含以下soap请求:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ndf="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
   <soapenv:Header/>
   <soapenv:Body>
      <ndf:LatLonListCityNames soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <displayLevel xsi:type="xsd:integer">1</displayLevel>
      </ndf:LatLonListCityNames>
   </soapenv:Body>
</soapenv:Envelope>
我不确定我在这里做错了什么。这里有什么指示吗

先谢谢你

我还尝试了以下代码:

import java.io.*;
import java.net.*;

public class AnotherSoapClient {
    public static void main(String[] args) throws Exception {

        String SOAPUrl      = "https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl";
        String xmlFile2Send = "<soapenv:Envelope xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\" xmlns:xsd=\\\"http://www.w3.org/2001/XMLSchema\\\" xmlns:soapenv=\\\"http://schemas.xmlsoap.org/soap/envelope/\\\" xmlns:ndf=\\\"https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl\\\">\\r\\n\" + \r\n" + 
                "               \"   <soapenv:Header/>\\r\\n\" + \r\n" + 
                "               \"   <soapenv:Body>\\r\\n\" + \r\n" + 
                "               \"      <ndf:LatLonListCityNames soapenv:encodingStyle=\\\"http://schemas.xmlsoap.org/soap/encoding/\\\">\\r\\n\" + \r\n" + 
                "               \"         <displayLevel xsi:type=\\\"xsd:integer\\\">1</displayLevel>\\r\\n\" + \r\n" + 
                "               \"      </ndf:LatLonListCityNames>\\r\\n\" + \r\n" + 
                "               \"   </soapenv:Body>\\r\\n\" + \r\n" + 
                "               \"</soapenv:Envelope>\";\r\n" + 
                "        String operation = \"LatLonListCityNames";

          String SOAPAction = "LatLonListCityNames";

        // Create the connection where we're going to send the file.
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;

        // Open the input file. After we copy it to a byte array, we can see
        // how big it is so that we can set the HTTP Cotent-Length
        // property. (See complete e-mail below for more on this.)

        byte[] b = xmlFile2Send.getBytes();

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty( "Content-Length",
                                     String.valueOf( b.length ) );
        httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction",SOAPAction);
        httpConn.setRequestMethod( "POST" );
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);

        // Everything's set up; send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();
        out.write( b );    
        out.close();

        // Read the response and write it to standard out.

        InputStreamReader isr =
            new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        String inputLine;

        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);

        in.close();
    }

  // copy method from From E.R. Harold's book "Java I/O"
  public static void copy(InputStream in, OutputStream out) 
   throws IOException {

    // do not allow other threads to read from the
    // input or write to the output while copying is
    // taking place

    synchronized (in) {
      synchronized (out) {

        byte[] buffer = new byte[256];
        while (true) {
          int bytesRead = in.read(buffer);
          if (bytesRead == -1) break;
          out.write(buffer, 0, bytesRead);
        }
      }
    }
  } 
}
import java.io.*;
导入java.net。*;
公共类另一个SOAPClient{
公共静态void main(字符串[]args)引发异常{
字符串SOAPUrl=”https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl";
字符串xmlFile2Send=“\\r\\n\”+\r\n“+
“\”\\r\\n\“+\r\n”+
“\”\\r\\n\“+\r\n”+
“\”\\r\\n\“+\r\n”+
“\”1\\r\\n\“+\r\n”+
“\”\\r\\n\“+\r\n”+
“\”\\r\\n\“+\r\n”+
“\”\”;\r\n“+
“字符串操作=\“LatLonListCityNames”;
字符串SOAPAction=“LatLonListCityNames”;
//创建我们要发送文件的连接。
URL=新URL(SOAPUrl);
URLConnection=url.openConnection();
HttpURLConnection httpConn=(HttpURLConnection)连接;
//打开输入文件。将其复制到字节数组后,我们可以看到
//它有多大,以便我们可以设置HTTP内容长度
//属性。(有关此属性的详细信息,请参阅下面的完整电子邮件。)
byte[]b=xmlFile2Send.getBytes();
//设置适当的HTTP参数。
httpConn.setRequestProperty(“内容长度”,
String.valueOf(b.length));
setRequestProperty(“内容类型”,“text/xml;charset=utf-8”);
httpConn.setRequestProperty(“SOAPAction”,SOAPAction);
httpConn.setRequestMethod(“POST”);
httpConn.setDoOutput(真);
httpConn.setDoInput(真);
//一切都设置好了;将读入的XML发送到b。
OutputStream out=httpConn.getOutputStream();
写出(b);
out.close();
//读取响应并将其写入标准输出。
输入流读取器isr=
新的InputStreamReader(httpConn.getInputStream());
BufferedReader in=新的BufferedReader(isr);
字符串输入线;
而((inputLine=in.readLine())!=null)
系统输出打印LN(输入线);
in.close();
}
//从E.R.Harold的书“Java I/O”中复制方法
公共静态无效副本(输入流输入,输出流输出)
抛出IOException{
//不允许其他线程从
//复制时输入或写入输出
//发生
已同步(in){
已同步(输出){
字节[]缓冲区=新字节[256];
while(true){
int bytesRead=in.read(缓冲区);
如果(字节读==-1)中断;
out.write(缓冲区,0,字节读取);
}
}
}
} 
}
对此,我得到以下错误:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    at AnotherSoapClient.main(AnotherSoapClient.java:41)
线程“main”java.net.ConnectException:连接超时:连接 位于java.net.DualStackPlainSocketImpl.connect0(本机方法) 位于java.net.DualStackPlainSocketImpl.socketConnect(未知源) 位于java.net.AbstractPlainSocketImpl.doConnect(未知源) 位于java.net.AbstractPlainSocketImpl.connectToAddress(未知源) 位于java.net.AbstractPlainSocketImpl.connect(未知源) 位于java.net.PlainSocketImpl.connect(未知源) 位于java.net.socksocketimpl.connect(未知源) 位于java.net.Socket.connect(未知源) 位于sun.security.ssl.SSLSocketImpl.connect(未知源) 位于sun.security.ssl.BaseSSLSocketImpl.connect(未知源) 位于sun.net.NetworkClient.doConnect(未知源) 位于sun.net.www.http.HttpClient.openServer(未知来源) 位于sun.net.www.http.HttpClient.openServer(未知来源) 位于sun.net.www.protocol.https.HttpsClient。(未知来源) 位于sun.net.www.protocol.https.HttpsClient.New(未知来源) 位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(未知源) 位于sun.net.www.protocol.http.HttpURLConnection.plainConnect0(未知源) 位于sun.net.www.protocol.http.HttpURLConnection.plainConnect(未知源) 位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(未知源) 位于sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(未知源) 位于sun.net.www.protocol.http.HttpURLConnection.getOutputStream(未知源) 位于sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(未知源) 位于AnotherSoapClient.main(AnotherSoapClient.java:41) 我可以通过浏览器或soap ui访问url。我不确定我在这里发生了什么


再次感谢您。

您的代码存在多个潜在问题。在未来,你应该坚持一个特定的问题和/或错误,你的帖子,这样我们可以回答清楚

用WSDL实现SOAP API可能很棘手,因为涉及到太多的元素。关于第一个错误示例:“前缀ndf…未绑定”- 此错误表示您违反了为此SOAP API定义的XML架构。你不能那样做。您需要提供/绑定ndf前缀或调整模式。这将修复XML解析器错误。此问题很常见,您可以在此处找到更多信息:

代码中导致ndf错误的主要问题:这是由于您从以下位置删除了ndf架构声明:

<soapenv:Envelope ...
我无法修复
import java.io.*;
import java.net.*;

public class AnotherSoapClient {
    public static void main(String[] args) throws Exception {

        String SOAPUrl      = "https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl";
        String xmlFile2Send = "<soapenv:Envelope xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\" xmlns:xsd=\\\"http://www.w3.org/2001/XMLSchema\\\" xmlns:soapenv=\\\"http://schemas.xmlsoap.org/soap/envelope/\\\" xmlns:ndf=\\\"https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl\\\">\\r\\n\" + \r\n" + 
                "               \"   <soapenv:Header/>\\r\\n\" + \r\n" + 
                "               \"   <soapenv:Body>\\r\\n\" + \r\n" + 
                "               \"      <ndf:LatLonListCityNames soapenv:encodingStyle=\\\"http://schemas.xmlsoap.org/soap/encoding/\\\">\\r\\n\" + \r\n" + 
                "               \"         <displayLevel xsi:type=\\\"xsd:integer\\\">1</displayLevel>\\r\\n\" + \r\n" + 
                "               \"      </ndf:LatLonListCityNames>\\r\\n\" + \r\n" + 
                "               \"   </soapenv:Body>\\r\\n\" + \r\n" + 
                "               \"</soapenv:Envelope>\";\r\n" + 
                "        String operation = \"LatLonListCityNames";

          String SOAPAction = "LatLonListCityNames";

        // Create the connection where we're going to send the file.
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;

        // Open the input file. After we copy it to a byte array, we can see
        // how big it is so that we can set the HTTP Cotent-Length
        // property. (See complete e-mail below for more on this.)

        byte[] b = xmlFile2Send.getBytes();

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty( "Content-Length",
                                     String.valueOf( b.length ) );
        httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction",SOAPAction);
        httpConn.setRequestMethod( "POST" );
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);

        // Everything's set up; send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();
        out.write( b );    
        out.close();

        // Read the response and write it to standard out.

        InputStreamReader isr =
            new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        String inputLine;

        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);

        in.close();
    }

  // copy method from From E.R. Harold's book "Java I/O"
  public static void copy(InputStream in, OutputStream out) 
   throws IOException {

    // do not allow other threads to read from the
    // input or write to the output while copying is
    // taking place

    synchronized (in) {
      synchronized (out) {

        byte[] buffer = new byte[256];
        while (true) {
          int bytesRead = in.read(buffer);
          if (bytesRead == -1) break;
          out.write(buffer, 0, bytesRead);
        }
      }
    }
  } 
}
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    at AnotherSoapClient.main(AnotherSoapClient.java:41)
<soapenv:Envelope ...
<ndf:LatLonListCityNames soapenv:encodingStyle ...