无法将HTTP标头添加到SOAP请求

时间:2013-08-16 16:22:36

标签: c# http soap

我已经查看了向SOAP请求添加HTTP标头的答案,并找到了一些好的并使用了代码。我想我在附加代码中已经完成了所有内容,但是,当我在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);
        }

2 个答案:

答案 0 :(得分:0)

我建议您使用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

答案 1 :(得分:0)

您需要确保在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);
                }