错误415适用于WSDL SOAP Web服务的Java客户端

huangapple go评论81阅读模式
英文:

Error 415 for WSDL SOAP Webservice Java client

问题

这一次我需要一些帮助解决我找不到问题所在的错误。

我开发了一个非常简单的Java代码来调用一个Web服务,但是当我运行它时,我得到了这个讨厌的错误:"Caused by: java.io.IOException: 服务器返回了 HTTP 响应代码: 415,针对的URL是: http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords"

在调查这个错误时,它说这与不支持的 Content-Type 有关,所以我下载了 SoapUI 5.6.0 来查看 Web 服务在等待什么,而它对我来说似乎是正确的。

这是我的代码:

package principal;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class PruebaWS {
    public void getNumber(int number){
        // ... 你的代码 ...
    }
    
    private Document parseXmlFile(String in){
        // ... 你的代码 ...
    }
}

这是运行时的错误:

Exception in thread "main" java.lang.RuntimeException:
java.io.IOException: 服务器返回了 HTTP 响应代码: 415,针对的URL是:
http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords
    at principal.PruebaWS.getNumber(PruebaWS.java:83) 	at
principal.Main.main(Main.java:6) Caused by: java.io.IOException:
服务器返回了 HTTP 响应代码: 415,针对的URL是:
http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords
    at
java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919)
    at
java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
    at principal.PruebaWS.getNumber(PruebaWS.java:67) 	... 1 more
C:\Local\NetBeans\Cache.1\executor-snippets\run.xml:111:
在执行此行时发生了以下错误:
C:\Local\NetBeans\Cache.1\executor-snippets\run.xml:68: Java
返回了: 1 BUILD FAILED (总时间: 2 秒)

任何帮助都将是宝贵的。

英文:

this time i will be needing some help with an error that i can't find where is the problem.

I developed a very simple java code to consume a web service but when I run it i'm getting this nasty error: "Caused by: java.io.IOException: Server returned HTTP response code: 415 for URL: http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords"

Investigating about this error it said that this has something to do with the Content-Type not been supported, so downloaded SoapUI 5.6.0 to see what the WS was waiting for and it seems correct to me.

Here is my code:

package principal;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class PruebaWS {
    public void getNumber(int number){
        String wsURL = "http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords";
        URL url = null;
        URLConnection connection = null;
        HttpURLConnection httpConn = null;
        String responseString = null;
        String outputString = null;
        ByteArrayOutputStream bout = null;
        OutputStream out = null;
        InputStreamReader isr = null;
        BufferedReader in = null;
        
        String xmlInput = 
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.dataaccess.com/webservicesserver/\">" +
                "   <soapenv:Header/>" +
                "   <soapenv:Body>" +
                "      <web:NumberToWords>" +
                "         <web:ubiNum>" + number +"</web:ubiNum>" +
                "      </web:NumberToWords>" +
                "   </soapenv:Body>" +
                "</soapenv:Envelope>";
        
        try {
            url = new URL(wsURL);
            connection = url.openConnection();
            httpConn = (HttpURLConnection) connection;
            
            byte[] buffer = new byte[xmlInput.length()];
            buffer = xmlInput.getBytes();
            
            String SOAPAction = "";
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("SOAPAction", SOAPAction);
            httpConn.setRequestProperty("Content-Length", String.valueOf(buffer.length));
            httpConn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
            httpConn.setRequestProperty("Accept", "*/xml");
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            httpConn.setUseCaches(false);
            
            out = httpConn.getOutputStream();
            out.write(buffer);
            out.close();
            
            // Lee la respuesta y escribe a un output estándard
            isr = new InputStreamReader(httpConn.getInputStream());
            in = new BufferedReader(isr);
            
            while ((responseString = in.readLine()) != null)
                outputString = outputString + responseString;
            
            System.out.println(outputString);
            System.out.println("");
            
            // Obtiene la respuesta desde la llamada al webService
            Document document = parseXmlFile(outputString);
            
            NodeList nodeList = document.getElementsByTagName("m:NumberToWordsResponse");
            String webServiceResponse = nodeList.item(0).getTextContent();
            System.out.println("La respuesta del web service es: " + webServiceResponse);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    private Document parseXmlFile(String in){
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(in));
            return db.parse(is);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}

And this is the error during Runtime:

> Exception in thread "main" java.lang.RuntimeException:
> java.io.IOException: Server returned HTTP response code: 415 for URL:
> http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords
> at principal.PruebaWS.getNumber(PruebaWS.java:83) at
> principal.Main.main(Main.java:6) Caused by: java.io.IOException:
> Server returned HTTP response code: 415 for URL:
> http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords
> at
> java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919)
> at
> java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
> at principal.PruebaWS.getNumber(PruebaWS.java:67) ... 1 more
> C:\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:111: The
> following error occurred while executing this line:
> C:\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:68: Java
> returned: 1 BUILD FAILED (total time: 2 seconds)

Any help will be valuable.

答案1

得分: 1

我在使用Postman时遇到了相同的问题,并且成功解决了。将Content-Type标头从application/xml更改为text/xml。

英文:

I ran into the same issue using Postman and was able to resolve. Change the Content-Type header from application/xml to text/xml

答案2

得分: 0

好的,经过相当长时间的调查,我发现这个错误与 Web 服务本身有关,我将其替换为另一个 Web 服务,没有任何问题。教训是:并非所有的网络服务都能正常工作。

英文:

Okay, so after investigating for quite some time I discovered that this error had something to do with the webservice it self, i swapped it with another and it worked with no issue. Lesson learned: NOT all webservices out there work as they should.

huangapple
  • 本文由 发表于 2020年7月24日 08:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定