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#.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
53 Points
246 Posts
How can I add attribute in header of a webservice?
Jun 26, 2019 12:57 AM|mywatermelon|LINK
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#.
Member
740 Points
321 Posts
Re: How can I add attribute in header of a webservice?
Jun 27, 2019 06:09 AM|Abraham Qian|LINK
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
Member
53 Points
246 Posts
Re: How can I add attribute in header of a webservice?
Jun 27, 2019 03:04 PM|mywatermelon|LINK
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:
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.
Member
740 Points
321 Posts
Re: How can I add attribute in header of a webservice?
Jun 28, 2019 02:02 AM|Abraham Qian|LINK
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
Member
53 Points
246 Posts
Re: How can I add attribute in header of a webservice?
Jun 28, 2019 02:48 AM|mywatermelon|LINK
Well, it works! Your way is just what I need, which is easier than my way. Thank you.