javax.xml.soap.SOAPException:无法从给定的源创建信封

时间:2019-02-01 10:48:26

标签: java xml soap

我有一个Java Soap客户端,但是当我调用它时,出现以下消息:

javax.xml.soap.SOAPException: Unable to create envelope from given source: 
Error on line -1 of document  : Premature end of file. Nested exception: 
Premature end of file.

这是我的Soap类的代码:

 public void doRequest(String service, String method, String xml)
  {
      this.service = service;
      this.method = method;

      String soap_message = 
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">" +
                "<soapenv:Header/>"+
                "<soapenv:Body>" +
                "<ns1:"+this.method+" xmlns:ns1=\"urn:"+this.service+"\" ns1=\"http://schemas.xmlsoap.org/soap/encoding\">" +
                "<msgstr xsi:type=\"xsd:string\">\""+xml+"\"</msgstr>" +
                "</ns1:"+this.method+">"+
                "</soapenv:Body>"+
                "</soapenv:Envelope>";

      try
      {
        URL endpoint = new URL(this.getURI());
        HttpURLConnection conn = (HttpURLConnection) endpoint.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Content-Type", "text/xml;");
        conn.setRequestProperty("SOAPAction", "urn:"+this.service+"#"+this.method);

        OutputStreamWriter  pw = new OutputStreamWriter(conn.getOutputStream());
        pw.write(soap_message);

        conn.connect();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = "";
        StringBuffer text = new StringBuffer();
        while ((line = in.readLine()) != null)
            text.append(line);

        this.response = text.toString();

      } catch(Exception e)
      {       
          this.response = e.getMessage();
      }
  }

我该如何解决该问题?我的代码有什么问题? 预先谢谢你

0 个答案:

没有答案