【发布时间】:2018-01-06 16:21:44
【问题描述】:
我尝试将XML 文件中的SOAP 消息发送到网络服务,然后获取二进制输出并对其进行解码。 Endpoint 使用HTTPS 协议,所以我在代码中使用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();
}
}
执行此操作时出现以下错误代码。
线程“main”java.io.IOException 中的异常:服务器返回 HTTP 响应代码:URL 403
【问题讨论】:
-
您应该提供凭据以获得访问权限。用于基本身份验证的 base64 中带有登录名/密码的身份验证标头,最简单的变体,但它取决于服务器端。您应该提供所需的服务器。
-
@user1516873 : 无需在此服务器上进行身份验证。
-
403-forbidden 表示请求已经到达服务器并且是有效的,但是服务器拒绝访问所请求的资源。总而言之,SSL 连接正常,因此您调用了错误的端点或 SOAP 标头中缺少凭据。
-
@pedrofb :我明白了,但是当我在带有 SoapUI 的 SOAP 请求中使用 XML 文件的内容时,我会从同一个端点得到响应。
-
如果内容相同,则检查并比较实际发送 SOAPUI 的标头和您的连接。有可能是服务器检测到了一些不正确,将其视为 403
标签: java xml web-services soap trusted-timestamp