haiduong87
haiduong87

Reputation: 366

FormUrlEncodedContent works but StringContent does not

I have a question about those 2 httpcontents.

I've made an webapi (php), copy from this: http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html I re-used the Rest.inc.php, and implement my own api file.

I try to call this api from .Net (I'm a .Net developer) - a simple WinForm Application - the FormUrlEncodedContent works but the StringContent does not. This my code:

Dictionary<string, string> parameters = new Dictionary<string, string>();
        parameters.Add("username", username);
        parameters.Add("title", title);
        parameters.Add("text", text);
        var jsonString = JsonConvert.SerializeObject(parameters);
        var content = new StringContent(jsonString, Encoding.UTF8, "text/json");
        //var content = new FormUrlEncodedContent(parameters);
        PostData(url, content);

And the reason why I want to use StringContent: in the real data, sometime the parameters will contain a byte[] (photo), and the FormUrlEncodedContent can't handle it

Please help me Thanks alot!

Upvotes: 1

Views: 2366

Answers (1)

Crowcoder
Crowcoder

Reputation: 11514

They are very different formats. If the api does not have a smart model binder like Asp.Net Web API then it will not work. You can always base64 encode your byte array which is the typical way to transmit bytes via HTTP.

Upvotes: 1

Related Questions