java - 如何在 Soap Request Java 中设置 header

标签 java web-services soap wsdl jax-ws

我在形成 SOAP 请求时遇到问题。

在该请求中,我应该在 header 部分而不是有效负载部分中添加用户名、密码和其他一些信息。

在 wsdl 条目下方

<wsdl:message name="InputUploadCustomerDocument_Headers">
<wsdl:part name="DocumentType" element="tns:DocumentType"/>
<wsdl:part name="FileName" element="tns:FileName"/>
<wsdl:part name="Password" element="tns:Password"/>
<wsdl:part name="PinNo" element="tns:PinNo"/>
<wsdl:part name="UserName" element="tns:UserName"/>
</wsdl:message>
<wsdl:message name="ReturnUploadCustomerDocument">
<wsdl:part name="parameters" element="tns:ReturnUploadCustomerDocument"/>
</wsdl:message>


<wsdl:operation name="UploadCustomerDocument">
<soap:operation soapAction="http://tempuri.org/ISend/UploadCustomerDocument" style="document"/>
<wsdl:input name="InputUploadCustomerDocument">
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="DocumentType" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="FileName" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="Password" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="PinNo" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="UserName" use="literal"/>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="ReturnUploadCustomerDocument">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>

下面的InputUploadCustomerDocument Java文件,该文件没有用户名,密码和其他字段,我需要在reuqest之前设置这些参数

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"fileData"
})
@XmlRootElement(name = "InputUploadCustomerDocument")
public class InputUploadCustomerDocument {

@XmlElement(name = "FileData", required = true)
protected byte[] fileData;

/**
 * Gets the value of the fileData property.
 * 
 * @return
 *     possible object is
 *     byte[]
 */
public byte[] getFileData() {
    return fileData;
}

/**
 * Sets the value of the fileData property.
 * 
 * @param value
 *     allowed object is
 *     byte[]
 */
public void setFileData(byte[] value) {
    this.fileData = value;
}

}

这是我需要调用的函数

@WebMethod(operationName = "UploadCustomerDocument", action = "http://tempuri.org/ISend/UploadCustomerDocument")
@WebResult(name = "ReturnUploadCustomerDocument", targetNamespace = "http://tempuri.org/", partName = "parameters")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public ReturnUploadCustomerDocument uploadCustomerDocument(
    @WebParam(name = "InputUploadCustomerDocument", targetNamespace = "http://tempuri.org/", partName = "parameters")
    InputUploadCustomerDocument parameters);

有人可以帮助我如何设置这些 header 吗?

最佳答案

由于您使用的是 JAX-WS,因此您可以在发出请求之前使用下面的行添加 header :

SOAPHeader header = envelope.addHeader();

有很多教程可以引用。转到 google 并搜索消费 SOAP Web 服务。这是一个这样的教程,您可以引用:

http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client/

这是您可以使用的另一个很好的示例: https://soa2world.blogspot.com/2009/05/direct-web-service-client-using-java.html

希望这有帮助。

关于java - 如何在 Soap Request Java 中设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42897047/

相关文章:

java - 如何在Netbeans的变量窗口中跳转到特定的调试点

web-services - Web 浏览器如何知道请求的 url 是 https 还是 http

c# - 用于移动应用程序的 .NET Web 服务

java - 用户证书身份验证无法正常工作的 HTTPS Apache CXF 上的 Soap

c# - 有没有办法从 ASP.NET WebMethod 中获取原始 SOAP 请求?

java - 添加命名空间前缀 Axis2

java - ChannelBuffer readInt()/writeInt() 导致错误值

java - 使用 tcp/ip 监视器监视 eclipse 上的其余调用

java - IntelliJ 列代码格式化

c# - 从 C# 调用 web 服务并发送未转义的 xml