【问题标题】:Error 415 for WSDL SOAP Webservice Java clientWSDL SOAP Webservice Java 客户端的错误 415
【发布时间】:2020-11-13 19:22:37
【问题描述】:

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

我开发了一个非常简单的 Java 代码来使用 Web 服务,但是当我运行它时,我收到了这个令人讨厌的错误:“原因:java.io.IOException:服务器返回 HTTP 响应代码:415 用于 URL:@987654321 @

调查这个错误,它说这与不支持的 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);
        }
    }
}

这是运行时的错误:

线程“主”java.lang.RuntimeException 中的异常: java.io.IOException:服务器返回 HTTP 响应代码:415 用于 URL: http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords 在 principal.PruebaWS.getNumber(PruebaWS.java:83) 在 principal.Main.main(Main.java:6) 引起:java.io.IOException: 服务器返回 HTTP 响应代码:415 用于 URL: http://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords 在 java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919) 在 java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515) 在 principal.PruebaWS.getNumber(PruebaWS.java:67) ... 还有 1 个 C:\Local\NetBeans\Cache\11.1\executor-sn-ps\run.xml:111: 执行此行时发生以下错误: C:\Local\NetBeans\Cache\11.1\executor-sn-ps\run.xml:68:Java 返回:1 BUILD FAILED(总时间:2 秒)

任何帮助都会很有价值。

【问题讨论】:

  • 您是否尝试过删除内容类型上的字符集部分以防万一...您也可以尝试使用“application/xml”作为 Content-Type 标头。如果问题仍然存在,我建议启用日志记录以检查正在发送的实际请求...stackoverflow.com/questions/1445919/…
  • 一点运气都没有,只是一遍又一遍地出现同样的错误......我无法对它自己的 WS 做任何事情,因为它不属于我,所以没有一个机会启用日志记录.
  • 您始终可以在客户端启用日志记录以检查您发送的内容

标签: java wsdl content-type soap-client http-status-code-415


【解决方案1】:

好的,所以在调查了一段时间后,我发现这个错误与它本身的 web 服务有关,我将它换成另一个,它没有任何问题。经验教训:并非所有网络服务都能正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2010-09-16
    • 2014-12-30
    • 1970-01-01
    • 2010-12-01
    • 2013-08-20
    • 1970-01-01
    相关资源
    最近更新 更多