java - SOAP 消息到 web 服务 - HTTP 响应代码 : 403 for URL

标签 java xml web-services soap trusted-timestamp

我尝试将 XML 文件中的 SOAP 消息发送到网络服务,然后获取二进制输出并对其进行解码。 Endpoint 使用 HTTPS 协议(protocol),所以我在代码中使用了 TrustManager 来避免 PKIX 问题。你可以在这里看到我的代码:

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

当我执行此操作时,出现以下错误代码。

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL

最佳答案

您的实现没问题,实际上问题与您的 Content-Type header 有关。

text/xml; charset=utf-8 是 SOAP 1.1 的默认 Content-Type,这可能不是您的版本。 SOAP 1.2 expects a header of type application/soap+xml; charset=utf-8 ,因此将您的代码行更改为下面的代码行将使其正常工作:

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

在 SoapUI 中,可以检查调用请求的 header 并转到窗口底部的 Headers 选项卡:

enter image description here

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

关于java - SOAP 消息到 web 服务 - HTTP 响应代码 : 403 for URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45410058/

相关文章:

java - 需要帮助理解这一行

java - 特定 url 的过滤器映射

angularjs - Angular 和 Ionic,$http 无法在 Android 真机上运行

java - JAXB minOccurs=0。元素存在还是不存在?

swift - 如何在 Swift 2 中调用 SOAP Web 服务?

java - 我需要 Axis 使用 J2EE 创建 Web 服务吗?

java - 如何注释自定义 Spring Boot 自定义存储库?

java - 如何在 Activity 之外编写带有 ProgressBar 的 Upload firebase 方法?

java - groovy 中的 Join() 不等待线程完成

php - 如何更改 DOM 中元素的名称?