0

我查看了将 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);
        }
4

2 回答 2

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\"");

更新:一个例子:如何使用 C# 发送/接收 SOAP 请求和响应?

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

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

于 2013-08-16T16:33:04.277 回答
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);
                }
于 2020-08-11T09:53:49.543 回答