使用 SAAJ 使用经过身份验证的 Web 服务?
Consuming an authenticated web service using SAAJ?
我正在尝试使用 SAAJ 使用经过身份验证的 Web 服务。这是我到目前为止的代码:
import java.io.ByteArrayOutputStream;
import javax.xml.soap.*;
import biz.source_code.base64Coder.*;
public class Client {
private static String endpoint = "https://example.com/xxx.php",
username = "xxx", password = "xxx";
private static SOAPMessage getRequest() throws Exception{
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
//set authorization as a HTTP header
String authorization = Base64Coder.encodeString(username + ":" + password);
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authorization);
//Call getReportList operation
return message;
}
public static void main(String[] args) throws Exception {
SOAPConnectionFactory connFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = connFactory.createConnection();
// create request message and give it content
SOAPMessage request = Client.getRequest();
// call the API endpoint with the request
SOAPMessage response = connection.call(request, endpoint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
String strMsg = new String(out.toByteArray());
System.out.println(strMsg);
}
}
当我 运行 这样做时,它打印 strMsg(来自 Web 服务的响应)如下:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>WSDL</faultcode><faultstring>SOAP-ERROR: Parsing WSDL: Couldn't load from '/www/example.wsdl' : failed to load external entity "/www/example.wsdl"
</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
我猜我已经验证了自己的身份,但网络服务有问题,不是我的。但是,我不完全确定。此错误消息似乎并不常见。
这是否意味着我提供的身份验证不正确或不充分?或者我是否必须提供 SSL 证书,因为 Web 服务使用 SSL?如果是,是否有关于如何将 SSL 证书与 SAAJ 一起使用的教程?
问题是我使用的是“https://example.com/xxx.php" as the endpoint not "https://example.com/xxx.php?wsdl”。这就是它无法加载任何 wsdl 文件的原因。
我正在尝试使用 SAAJ 使用经过身份验证的 Web 服务。这是我到目前为止的代码:
import java.io.ByteArrayOutputStream;
import javax.xml.soap.*;
import biz.source_code.base64Coder.*;
public class Client {
private static String endpoint = "https://example.com/xxx.php",
username = "xxx", password = "xxx";
private static SOAPMessage getRequest() throws Exception{
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
//set authorization as a HTTP header
String authorization = Base64Coder.encodeString(username + ":" + password);
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authorization);
//Call getReportList operation
return message;
}
public static void main(String[] args) throws Exception {
SOAPConnectionFactory connFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = connFactory.createConnection();
// create request message and give it content
SOAPMessage request = Client.getRequest();
// call the API endpoint with the request
SOAPMessage response = connection.call(request, endpoint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
String strMsg = new String(out.toByteArray());
System.out.println(strMsg);
}
}
当我 运行 这样做时,它打印 strMsg(来自 Web 服务的响应)如下:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>WSDL</faultcode><faultstring>SOAP-ERROR: Parsing WSDL: Couldn't load from '/www/example.wsdl' : failed to load external entity "/www/example.wsdl"
</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
我猜我已经验证了自己的身份,但网络服务有问题,不是我的。但是,我不完全确定。此错误消息似乎并不常见。
这是否意味着我提供的身份验证不正确或不充分?或者我是否必须提供 SSL 证书,因为 Web 服务使用 SSL?如果是,是否有关于如何将 SSL 证书与 SAAJ 一起使用的教程?
问题是我使用的是“https://example.com/xxx.php" as the endpoint not "https://example.com/xxx.php?wsdl”。这就是它无法加载任何 wsdl 文件的原因。