1

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!

haiduong87
  • 316
  • 3
  • 14

1 Answers1

1

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.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • Can you make it more clearly... In my case, the FormUrlEncodedContent has worked, but if I use an encodetring from a byte[], it said the uir is too long Here: http://stackoverflow.com/questions/23703735/how-to-set-large-string-inside-httpcontent-when-using-httpclient So I change to StringContent, as that post said... – haiduong87 Oct 10 '16 at 15:38
  • Do you want to use JSON or Form Encoding? Either way you will have to change your PHP service. – Crowcoder Oct 10 '16 at 15:47
  • I think json is ok ~~ About the php service, how can I change it? I'm not a php developer, just try to find a solution for my case... – haiduong87 Oct 10 '16 at 16:36
  • @haiduong87 I'm not a PHP dev either, but I'm sure there are libraries for working with JSON. – Crowcoder Oct 10 '16 at 18:30
  • I've tried restsharp It's ok now But still don't know how it works :) – haiduong87 Oct 11 '16 at 03:45