簡體   English   中英

WSDL SOAP Web 服務 Java 客戶端的錯誤 415

[英]Error 415 for WSDL SOAP Webservice Java client

這次我需要一些幫助來解決我找不到問題所在的錯誤。

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"

調查此錯誤時,它說這與不支持 Content-Type 有關,因此下載了 SoapUI 5.6.0 以查看 WS 正在等待什么,這對我來說似乎是正確的。

這是我的代碼:

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

這是運行時的錯誤:

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在 java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.Z93 F725A07423FE1C889F448B33D21F46Z: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 (總時間: 2 秒)

任何幫助都是有價值的。

我使用 Postman 遇到了同樣的問題並且能夠解決。 將 Content-Type header 從 application/xml 更改為 text/xml

好的,所以在調查了很長一段時間后,我發現這個錯誤與它自己的 web 服務有關,我將它與另一個交換,它沒有任何問題。 經驗教訓:並非所有網絡服務都可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM