Last post Jun 28, 2019 02:48 AM by mywatermelon
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#.
Jun 27, 2019 06:09 AM|Abraham Qian|LINK
We could use OperationContext to add bespoke message header to the SOAP message, please
Refer to the MSDN thread.
We could also use the IClientMessageInsepctor interface to create a persistent message header, please refer to the below link.
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:
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;
Stream putStream = request.GetRequestStream();
putStream.Write(postData, 0, postData.Length);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
string ContextToReturn = reader.ReadToEnd();
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.
Jun 28, 2019 02:02 AM|Abraham Qian|LINK
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.
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.