到 webservice 的 SOAP 消息 - HTTP 响应代码:403 表示 URL

我尝试将文件中的消息发送到Web服务,然后获取二进制输出并对其进行解码。终结点使用协议,因此我在代码中使用了协议以避免出现问题。你可以在这里看到我的代码:SOAPXMLHTTPSTrustManagerPKIX

import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;

public class Main{
    public static void sendSoapRequest() throws Exception {
        String SOAPUrl = "URL HERE";
        String xmlFile2Send = ".\\src\\request.xml";
        String responseFileName = ".\\src\\response.xml";
        String inputLine;

        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
            public void checkClientTrusted(X509Certificate[] certs, String authType) { }
            public void checkServerTrusted(X509Certificate[] certs, String authType) { }

        } };

        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) { return true; }
        };
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

        // Create the connection with http
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;
        FileInputStream fin = new FileInputStream(xmlFile2Send);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        copy(fin, bout);
        fin.close();

        byte[] b = bout.toByteArray();
        StringBuffer buf=new StringBuffer();
        String s=new String(b);

        b=s.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", "");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);

        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();

        // Read the response.
        httpConn.connect();
        System.out.println("http connection status :"+ httpConn.getResponseMessage());
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        FileOutputStream fos=new FileOutputStream(responseFileName);
        copy(httpConn.getInputStream(),fos);
        in.close();
    }

    public static void copy(InputStream in, OutputStream out) throws IOException {

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

    public static void main(String args[]) throws Exception {
        sendSoapRequest();
    }
}

当我执行此命令时,我收到以下错误代码。

线程“main” java.io.IOException 中的异常:服务器返回 HTTP 响应代码:URL 为 403


答案 1

您的实现很好,实际上问题与您的标头有关。Content-Type

该值是 SOAP 1.1 的默认值,它可能不是您的版本。SOAP 1.2 需要一个 application/soap+xml 类型的标头;charset=utf-8,因此将代码行更改为下面的代码行将使其正常工作:text/xml; charset=utf-8Content-Type

httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");

在 SoapUI 中,可以检查调用请求的标头,然后转到窗口底部的“标头”选项卡:

enter image description here

然后,您可以比较应用程序配置与 SoapUI 配置之间的差异。


答案 2

403 错误可能与发送到服务器的 soap 请求标头有关。所有有效的主机将允许您的 Java 应用程序信任 URL 的 SSL 证书。检查您的服务器是否期望使用用户名/密码的 soap 标头。如果您有权访问此服务器,则可以通过 Web 服务器日志检查请求失败的位置。错误代码指向缺少 Soap 标头,尤其是具有用户名和密码的 Soap 标头


推荐