SOAP消息到webservice - HTTP响应代码:403用于URL [英] SOAP message to webservice - HTTP response code: 403 for URL
问题描述
我尝试将 XML
文件中的 SOAP
消息发送到Web服务,然后获取二进制输出和解码它。端点使用 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;
公共类Main {
public static void sendSoapRequest()throws Exception {
String SOAPUrl =URL HERE;
String xmlFile2Send =.\\src\\request.xml;
String responseFileName =.\\src\\ responsese.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());
//创建所有信任的主机名验证者
HostnameVerifier allHostsValid = new HostnameVerifier(){
public boolean verify(String hostname,SSLSession session){return true; }
};
//安装所有信任的主机验证程序
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
//使用http
创建连接URL url =新URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn =(HttpURLConnection)连接;
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();
//设置适当的HTTP参数。
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();
//阅读回复。
httpConn.connect();
System.out.println(http连接状态:+ 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)抛出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
响应代码:403 for URL
您的实施没问题,问题与您的内容类型有关
标题,实际上。
值 text / xml; charset = utf-8
是SOAP 1.1的默认 Content-Type
,可能不是你的版本。
然后,您可以比较应用程序配置和SoapUI配置之间的差异。
I try to send a SOAP
message in an XML
file to a webservice and than grab the binary output and decode it. Endpoint uses HTTPS
protocol, so I used TrustManager
in my code to avoid PKIX
problems. You can see my code here:
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();
}
}
I get following error code, when I execute this.
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL
Your implementation is alright, the problem is related to your Content-Type
header, in fact.
The value text/xml; charset=utf-8
is the default Content-Type
of SOAP 1.1, which is probably not the version of yours. SOAP 1.2 expects a header of type application/soap+xml; charset=utf-8
, so changing your line of code to this one below is gonna make it working:
httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
In SoapUI, it's possible to check the headers calling the request and going to the Headers tab on the bottom of the window:
Then, you can compare the differences between your application configs and the SoapUI ones.
这篇关于SOAP消息到webservice - HTTP响应代码:403用于URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!