I have two ASP.NET MVC web applications deployed under IIS8 (let me call them sender and receiver web applications). I am calling an action method inside the receiver web application from an action method inside sender.
Now inside the sender, I have the following action method that will upload a string
to an 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 am encoding the string
using UTF-8 before uploading it, since I will be passing unicode characters such as £
, ¬
, etc...
On the receiver web application, the receiving action method is as follows:
public List<CRUDOutput> CreateResource(Resource resourceinfo)
At the beginning I thought my approach would not work well. Since I am sending an encoded data (using wc.Encoding = System.Text.Encoding.UTF8;
) from the sender to the receiver action method, and I am not doing any kind of decoding on the receiver action method.
However, it seems on the receiving action method the resourceinfo
received the correct decoded values. So it seems like ASP.NET MVC will handle the decoding automatically somewhere.
First question:
Can anyone tell me how ASP.NET MVC handles the decoding inside the receiving action 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");
Yet, it seems that this does not have any effect in my case. When i remove the above line of code, nothing changes. Can anyone tell me if defining the content type header will have any effect in my case?
Last question:
If I do not explicitly define the UTF-8 encoding using wc.Encoding = System.Text.Encoding.UTF8;
will WebClient()
use a default encoding?