I have two ASP.NET MVC web applications deployed in IIS8 (let me call them sender and receiver web applications). I call the action method inside the recipient's web application from the action method inside the sender.
Now, inside the sender, I have the following action method that will output string to the external action method on the receiver:
using (WebClient wc = new WebClient()) { var data = JsonConvert.SerializeObject(resource); string url = "https://receiver/CreateResource?AUTHTOKEN=" + pmtoken; Uri uri = new Uri(url); wc.Encoding = System.Text.Encoding.UTF8; wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8"); wc.Headers.Add("Authorization", token); output = wc.UploadString(uri, data) }
I encode string with UTF-8 before loading it, since I will pass Unicode characters like £ , ¬ etc.
In the recipient’s web application, the receive action method is as follows:
public List<CRUDOutput> CreateResource(Resource resourceinfo)
In the beginning, I thought my approach would not work well. Since I am sending encoded data (using wc.Encoding = System.Text.Encoding.UTF8; ) from the sender to the receiver's action method, and I am not doing any decoding in the receiver's action method.
However, by the receive method, resourceinfo received the correct decoded values. So it looks like ASP.NET MVC will handle decoding automatically somewhere.
First question:
Can someone tell me how ASP.NET MVC handles decoding inside the receiving method?
Second question:
Inside my WebClient() method, I define the following to specify the content type header:
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
However, this does not seem to have any effect in my case. When I delete the above line of code, nothing changes. Can someone tell me if the definition of the header of the content type will have any effect in my case?
Last question:
Unless I explicitly define the encoding of UTF-8 with wc.Encoding = System.Text.Encoding.UTF8; will WebClient() use the default encoding?