【发布时间】:2015-03-05 10:31:01
【问题描述】:
我有网址为http://192.168.0.10/services/abc?wsdl的网络服务
此 Web 服务器使用摘要身份验证,用户名为 admin,密码为 admin
我想将请求跟随发送到此服务器
SOAP 请求 XML 是 SOAP_RQ.XML
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="LogsGet" xmlns:mal="MalteseGlobal" xmlns:job="JobGlobal">
<soapenv:Body>
<log:LogsGetReq Cmd="Start" OpV="01.00.00" Sev="Info to critical"/>
</soapenv:Body>
</soapenv:Envelope>
我的代码:
private static SOAPMessage createSOAPRequest(String username, String password) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("log", "LogsGet");
envelope.addNamespaceDeclaration("mal", "MalteseGlobal");
envelope.addNamespaceDeclaration("job", "JobGlobal");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("LogsGetReq", "log");
QName Cmd = new QName("Cmd");
QName OpV = new QName("OpV");
QName Sev = new QName("Sev");
soapBodyElem.addAttribute(Cmd, "Start");
soapBodyElem.addAttribute(OpV, "01.00.00");
soapBodyElem.addAttribute(Sev, "Info to critical");
//SOAP Header
MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("UsernameToken", username);
hd.addHeader("PasswordText", password);
soapMessage.saveChanges();
return soapMessage;
}
public void sendSoapRequest(String url, String username, String password) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(username, password, txtArea), url);
// Process the SOAP Response
ByteArrayOutputStream bos = new ByteArrayOutputStream();
soapResponse.writeTo(bos);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.out.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
当我发送请求时,我收到以下消息: 错误响应:(需要 401 授权)
如果我发送请求,请使用 curl 工具 (http://curl.haxx.se/download/curl-7.41.0.zip)
命令行: curl.exe -X POST http://192.168.0.10/services/Maltese -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:LogsGet" --digest -u admin:admin -d @SOAP_RQ.xml -v
我收到消息回复正常。
任何人都可以帮助我,如何使用 JAVA(或 C#)通过 HTTP 发送 SOAP 请求?
谢谢
【问题讨论】:
标签: java xml soap wsdl digest-authentication