简体   繁体   English

如何将SOAP标头从客户端传递到Web服务

[英]How to pass a SOAP header from a client to a web service

I have a client in C# and the Web Service must receive a certificate header. 我在C#中有一个客户端,并且Web服务必须接收证书标头。 I don't know how to add the header to the Web Service. 我不知道如何将标头添加到Web服务。 This is my code: 这是我的代码:

// Define a SOAP header by deriving from the SoapHeader base class.
// The header contains just one string value.
public class MyHeader : SoapHeader
{
    public string MyValue;
}

public partial class Service: System.Web.UI.Page
{
    MyWebService ESClient = new MyWebservice();
    private static String TAG_CERTIFICATE = "MxIFZjFCBFa...";
    protected void Page_Load(object sender, EventArgs e)
    {
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
        CredentialCache cache = new CredentialCache();

        MyHeader header = new MyHeader();

        // Populate the values of the SOAP header.
        header.MyValue = TAG_CERTIFICATE;

        //HOW CAN I ADD THIS CERTIFICATE TO MY ESCLIENT WEB SERVICE?????

        //remSolSal is the response of the Web Service
        remSolSal response= new remSolSal();
        //remDatSol is the method of the Web Service and getRemSolEnt the parameters that I send in another function
        response = ESClient.remDatSol(getRemSolEnt());
    }

The XML model certificate: XML模型证书:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sol="url...">
    <soapenv:Header>
        <certificate>
            JgsJSP6Ql8f........
        </certificate>

Also, I have a client in Java that makes what I have to do in C#. 另外,我有一个Java客户端,可以使用C#进行操作。 It uses a this funtion to add the header, where paramName is "certificate" and paramContent is the value "JgsJSP6Ql8f........": 它使用此功能添加标头,其中paramName为“ certificate”,paramContent为值“ JgsJSP6Ql8f ........”:

 /**
 * This method adds a custom header to message.
 * 
 * @param paramName
 */
private static void addHeaderParam(String paramName, String paramContent) {
    try {
        List<Header> headers = new ArrayList<Header>();
        Header dummyHeader = new Header(new QName(TARGETNAMESPACE,
                paramName), paramContent, new JAXBDataBinding(String.class));
        headers.add(dummyHeader);

        // client side:
        ((BindingProvider) port).getRequestContext().put(
                Header.HEADER_LIST, headers);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

There are many, many ways to achieve what you're asking for and it depends on how your client is set up and how you want to maintain this. 实现需求的方法有很多,这取决于客户的设置方式以及维护方式。 Are you using a service reference? 您是否在使用服务参考? Do you want to manage this in configuration or code? 您要通过配置还是代码来管理? The easiest way to just plunk in a custom header is to use the <headers> element in your endpoint configuration in your config as referenced here: http://msdn.microsoft.com/en-us/library/ms731749(v=vs.110).aspx 插入自定义标头的最简单方法是在配置中使用终结点配置中的<headers>元素,如此处所引用: http : //msdn.microsoft.com/zh-cn/library/ms731749(v=vs 0.110)的.aspx

I personally recommend not putting the definition of your cert in your app but storing it in your server's keystore. 我个人建议不要将证书的定义放在应用程序中,而是将其存储在服务器的密钥库中。 You can then reference the certificate in a behavior applied to your client. 然后,您可以在应用于客户端的行为中引用证书。 You can also pull out the certificate with code and do what you need to by referencing the ESClient.ClientCredentials object. 您还可以使用代码提取证书,并通过引用ESClient.ClientCredentials对象执行所需的操作。

Finally, I used the class ClientMessageInspector to modify SOAP message: 最后,我使用类ClientMessageInspector修改SOAP消息:

public class ClientMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector
{
    #region IClientMessageInspector Members

     public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
     {

     }

     public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
     {
         CustomMessageHeader header = new CustomMessageHeader();

         request.Headers.Add(header);

         return null;
     }

     #endregion
 }

/// <summary>
/// Represents a custom message header.
/// </summary>
public class CustomMessageHeader : MessageHeader
{
    private const string HeaderName = "CustomHeader";
    private const string HeaderNamespace = "";

    /// <summary>
    /// Gets the name of the message header.
    /// </summary>
    /// <returns>The name of the message header.</returns>
    public override string Name
    {
        get { return HeaderName; }
    }

    /// <summary>
    /// Gets the namespace of the message header.
    /// </summary>
    /// <returns>The namespace of the message header.</returns>
    public override string Namespace
    {
        get { return HeaderNamespace; }
    }

    /// <summary>
    /// Called when the header content is serialized using the specified XML writer.
    /// </summary>
    /// <param name="writer">
    /// An <see cref="T:System.Xml.XmlDictionaryWriter" /> that is used to serialize the header contents.
    /// </param>
    /// <param name="messageVersion">
    /// The object that contains information related to the version of SOAP associated with a message and its exchange.
    /// </param>
    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    {
        writer.WriteElementString("certificate", "JgsJSP6Ql8f........");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM