locked
How can I add attribute in header of a webservice? RRS feed

  • Question

  • User1052024640 posted

    I need to get context from a webservice which provide by our customer.

    I add the webservice successfully.

    Here is the code which to get the context:

    ServiceReference1.CxfWebServiceApiClient Client = new ServiceReference1.CxfWebServiceApiClient();                                
    ServiceReference1.sysUserOfWs SUOW=Client.getUserInfo("txhxl5h31111a", "123456");

    Well, it failed and threws an error which let me contact my customer.

    The customer told me that I need to add an attribute 'Code=123456' to the header or it must be failed.

    Meanwhile, I know less about the webservice and I found someone using the HttpPost/HttpWebRequest or any other method to call the webservice in some tutorial.

    I wanna know if my way is right and how to add an attribute in header of a webservice? Thank you.

    ---------------------------------------

    What's more, I tried my customer's way in Postman and it works. It seems now the problem is how to achieve this in C#.

    Wednesday, June 26, 2019 12:57 AM

Answers

  • User-330142929 posted

    Hi,

    Your way is correct, I misunderstand the question, you need the HTTP header instead of the soap message header. Anyway, I am glad that you can solve the problem. Besides, if you consume the service without the way of using Httpwebquest(Httpclient class),  we could use WebOperationContext class to add the Http request header. provided we use a client proxy class(adding service reference) or a channel factory to invoke the service. please refer to the link.

    https://stackoverflow.com/questions/13856362/adding-http-request-header-to-wcf-request

    Best Regards

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2019 2:02 AM

All replies

  • User-330142929 posted

    Hi,

    We could use OperationContext to add bespoke message header to the SOAP message, please
    Refer to the MSDN thread.
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ad67b8e-b6ea-439e-8acb-b6e62248cfe2/how-can-i-add-attribute-in-header-of-a-webservice?forum=wcf
    We could also use the IClientMessageInsepctor interface to create a persistent message header, please refer to the below link.
    https://stackoverflow.com/questions/55005505/how-to-pass-winform-custom-user-credentials-to-wcf-services-in-every-requests/55038793#55038793

    Best Regards

    Abraham

    Thursday, June 27, 2019 6:09 AM
  • User1052024640 posted

    The MSDN thread is just posted by me also.

    Now I solve it in a stupid way as below:

    Firstly, I used the Wizdler of Chrome to get the XML of the request method.

    Secondary, I used a HttpWebRequest to post the XML with a custom header.

    Finally, using a StreamReader to get the GetResponseStream.

    Just like this:

    byte[] postData = Encoding.Default.GetBytes(XMLContext);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.asp.net");
                request.Method = "POST";
                request.ContentType = "application/xml";
                request.Accept = "application/xml";
                request.ContentLength = postData.Length;
                request.Headers.Add("code", "123456");
    
                Stream putStream = request.GetRequestStream();
                putStream.Write(postData, 0, postData.Length);
                putStream.Close();
    
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string ContextToReturn = reader.ReadToEnd();
                    return ContextToReturn;
                }

    I don't know if it is the best way. However, I feel troublesome for I have to get the XML of every request method.

    While there is any better way, please tell me. Thank you.

    Thursday, June 27, 2019 3:04 PM
  • User-330142929 posted

    Hi,

    Your way is correct, I misunderstand the question, you need the HTTP header instead of the soap message header. Anyway, I am glad that you can solve the problem. Besides, if you consume the service without the way of using Httpwebquest(Httpclient class),  we could use WebOperationContext class to add the Http request header. provided we use a client proxy class(adding service reference) or a channel factory to invoke the service. please refer to the link.

    https://stackoverflow.com/questions/13856362/adding-http-request-header-to-wcf-request

    Best Regards

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2019 2:02 AM
  • User1052024640 posted

    Well, it works! Your way is just what I need, which is easier than my way. Thank you.

    Friday, June 28, 2019 2:48 AM