java - 服务器无法识别 HTTP header SOAPAction : 的值

标签 java web-services soap wsdl

我正在尝试使用 SAAJ 为这个 WSDL 调用 SOAP 网络服务。我在此处提交了对此 WSDL 的请求 http://www.webservicex.net/ws/WSDetails.aspx?WSID=64,它生成了以下 SOAP 请求..

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetGeoIP xmlns="http://www.webservicex.net/">
      <IPAddress>string</IPAddress>
    </GetGeoIP>
  </soap:Body>
</soap:Envelope>

我使用 SAAJ 生成了相同的 SOAP 请求并提交了它,但收到此错误 - 服务器无法识别 HTTP header SOAPAction 的值:。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Header/>
<SOAP-ENV:Body>
<GetGeoIP xmlns="http://www.webservicex.net/">
<IPAddress>SUNW</IPAddress>
</GetGeoIP>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Java 代码:

package newpackage;

import javax.xml.namespace.QName;
import javax.xml.soap.*;

public class SOAPClientSAAJ {

    public static void main(String args[]) throws Exception {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "http://www.webservicex.net/geoipservice.asmx";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        // print SOAP Response
        System.out.print("Response SOAP Message:");
        soapResponse.writeTo(System.out);

        soapConnection.close();
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://www.w3.org/2001/XMLSchema";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("xsd", serverURI);

        SOAPBody body = soapMessage.getSOAPBody();
        QName bodyName = new QName("http://www.webservicex.net/", "GetGeoIP" );
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
        QName name = new QName("IPAddress");
        SOAPElement symbol = bodyElement.addChildElement(name);
        symbol.addTextNode("SUNW");
        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
    }

}

最佳答案

添加

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", "http://www.webservicex.net/" + "GetGeoIP");

关于java - 服务器无法识别 HTTP header SOAPAction : 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26248323/

相关文章:

java - 如何在运行时动态生成ContextMenu项目?

java - JTable Swing 检索数据

java - 返回 404 的 Rest Web 服务

java - 在JDK 1.6上开发webservice客户端,需要兼容JDK 1.5.03

rest - 通过 SAP Business ByDesign 上的 API 更新由客户支付的供应商发票

java - android 中最喜欢的按钮无法正常工作

java - 读取jboss记录器文件(获取绝对路径)

c# - SQL CLR Web 服务调用 : Limiting Overhead

iphone - 我在 Cocoa 的 SOAP XML 中使用什么数据类型作为整数?

.net - 编码和解码内容类型 'application/soap+msbin1',以便与 C#.NET 中的 HttpWebRequest/HttpWebResponse 一起使用