【问题标题】:can't add HTTP header to SOAP request无法将 HTTP 标头添加到 SOAP 请求
【发布时间】:2013-08-16 16:22:36
【问题描述】:

我查看了将 HTTP 标头添加到 SOAP 请求的答案,并找到了一些好的答案并使用了代码。我认为附加代码中的所有内容都正确,但是,当我在 Fiddler 中查看请求时,我看不到任何添加的标头。有人可以看看我是否在这里遗漏了什么吗?谢谢。这是一项 PeopleSoft 服务。

UTZ_EMP_BIO_INFO_PortTypeClient utz = new UTZ_EMP_BIO_INFO_PortTypeClient();
UTZ_EMP_BIO_INFO_PortType utShare = utz;


using (System.ServiceModel.OperationContextScope scope = new System.ServiceModel.OperationContextScope((IContextChannel)utz.InnerChannel))
       {
            MessageHeaders messageHeadersElement = System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders;
            messageHeadersElement.Add(MessageHeader.CreateHeader("SOAPAction", String.Empty, "UTZ_EMP_BIO_INFO.v1"));

           Console.WriteLine("down under");
           SendEmpBioRespV1 resp = default(SendEmpBioRespV1);
           rqst.GetEmpBioInfoReq.GetEmpBioInfo.UTZ_EMP_SRCH_VW.SSN = "123456789";
           rqst.GetEmpBioInfoReq.GetEmpBioInfo.UTZ_EMP_SRCH_VW.EMPLID = "";
           resp = utShare.UTZ_EMP_BIO_INFO(rqst);
           Console.WriteLine(resp.SendEmpBioResp.SendEmpBioInfo.UTZ_EMP_BIO_WRK.CITY);
        }

【问题讨论】:

    标签: c# http soap


    【解决方案1】:

    我建议你使用 HttpWebRequest 类:

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/");

    然后就可以调用Headers.Add

    myReq.Headers.Add("SOAPAction", "\"http://www.contoso.com/Action\"");

    更新:一个例子:How to send/receive SOAP request and response using C#?

    但是

    根据你的例子,我会使用类似的东西:

    using (System.ServiceModel.OperationContextScope scope = new System.ServiceModel.OperationContextScope((IContextChannel)utz.InnerChannel))
    {
        MessageHeaders messageHeadersElement =  MessageHeader.CreateHeader("SOAPAction", String.Empty, "UTZ_EMP_BIO_INFO.v1");
    
        OperationContext.Current.OutgoingMessageHeaders.Add(messageHeadersElement);
    etc...
    

    取自: http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontextscope.aspx

    【讨论】:

    • HttpWebRequest 有什么关系?
    • 我想我认为这个问题是关于“将 HTTP 标头添加到 SOAP 请求”,我会使用 HttpWebRequest 来处理 SOAP 请求。 stackoverflow.com/questions/2434703/… 也许我理解错了这个问题,如果我理解了,我深表歉意。如果对您不起作用,请不要选择/投赞成票。您会推荐 John 什么?
    • @JohnSaunders?上一条评论。
    • 我建议编写有效的代码。你的代码不会做任何事情。
    • 我尝试了“根据您的示例,我会使用类似的东西”之后的代码,我得到了相同的结果 - 没有添加 http 标头。我很困惑,虽然我是新手。我只是想将“SOAPAction:UTZ_EMP_BIO_INFO.v1”添加到 SOAP 消息的 http 标头中。谢谢。
    【解决方案2】:

    您需要确保在 using 语句中调用服务方法。

       using (OperationContextScope scope = new OperationContextScope(service.InnerChannel))
                    {
                            HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                            requestMessage.Headers["key"] = "value";
                            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                        
                        //invoke the service method
                        var serviceResponse = service.YourMethod(request);
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多