gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:45:45 24 4
gpt4 key购买 nike

我尝试将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com