0

I've looked at answers to adding an HTTP header to a SOAP request, and found some good ones and used the code. I think I have everything right in the attached code, but, when I look at the request in Fiddler, I can't see any header being added. Can someone look and see if I am missing something here? THank you. It is a PeopleSoft service.

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);
        }
Ehsan
  • 31,833
  • 6
  • 56
  • 65

2 Answers2

0

I would recommend that you use the HttpWebRequest class:

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

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

Then you can call Headers.Add

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

UPDATE: an example: How to send/receive SOAP request and response using C#?

But

Based on your example I would use something like:

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...

Taken from: http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontextscope.aspx

Community
  • 1
  • 1
  • What does the `HttpWebRequest` have to do with anything? – John Saunders Aug 16 '13 at 19:57
  • I guess I assumed that this question was regarding "adding an HTTP header to a SOAP request", I would use `HttpWebRequest` for a SOAP request. http://stackoverflow.com/questions/2434703/using-the-httpwebrequest-class Maybe I understood the question incorrectly and if I did, I apologize. Do not select/up vote if it doesn't work for you.What would you recommend John? – RobertMcAtee Aug 16 '13 at 20:17
  • @JohnSaunders? for the previous comment. – RobertMcAtee Aug 16 '13 at 20:22
  • I recommend writing code that works. Your code won't do anything. – John Saunders Aug 16 '13 at 20:37
  • I tried the code that followed "Based on your example I would use something like" and I get the same results - no http header added. I'm stumped, although I am a newbie. I am just trying to add "SOAPAction: UTZ_EMP_BIO_INFO.v1" to the http headers for the SOAP message. Thanks. – Gilbert Wildin Aug 16 '13 at 20:58
  • @JohnSaunders That was mature. "It is not the critic who counts; not the man who points out how the strong man stumbles, or where the doer of deeds could have done them better. The credit belongs to the man who is actually in the arena..." Would you like to offer an answer? – RobertMcAtee Aug 16 '13 at 20:58
0

You need to make sure that you invoke the service method inside the using statement.

   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);
                }
Prakash
  • 789
  • 6
  • 9