I'm trying to process a post request sent from C # code. But so far the working solution has failed ...

I tried to do this:

var values = new Dictionary<string, string> { { "kod", "111" }, { "tupe", "0" }, { "id_user", user.Id }, { "image", encodedFile }, // файл закодированный в base64 { "title", "654321" }, { "text", "1234526" } }; var jsonString = JsonConvert.SerializeObject(values); var content = new System.Net.Http.StringContent(String, Encoding.UTF8, "application/json"); var response = await client.PostAsync("http://.php", content); var responseString = await response.Content.ReadAsStringAsync(); 

But in this dispatch, I never figured out how to receive it not on the server. Because of "application / json".

Next tried this:

  var String = "kod=111&tape=0&id_user=" + user.Id + "&image=" + encodedFile + "&title=123&text=dfgdg"; var content = new System.Net.Http.StringContent(String, Encoding.UTF8, "application/json"); var response = await client.PostAsync("http://plus-you.ru/mobile/tape_v_2.php", content); var responseString = await response.Content.ReadAsStringAsync(); 

But now, when received on the server in the coded text, for some reason, some characters changed, making it impossible to decode the file back ...

Found many options with WebClient for example. In my Visual Studio 2015, for some reason, WebClient is not recognized at all ...

I need to either figure out how to handle this Json using PHP on the server, or figure out how to send so that the base64 characters do not change, or some other guidance on the true path ...

    1 answer 1

    The point is in application/json (by the way, there was another similar question on so , so I will not repeat the explanation)

    Try this bundle:

    On c #, make a query like this:

     var req = (HttpWebRequest)WebRequest.CreateHttp("https://server/api/v1/method/"); req.Timeout = 1000 * 60 * 60; /* 1 hour */ req.Method = "POST"; req.ContentType = "application/json"; using (var s = req.GetRequestStream()) { var data = Encoding.UTF8.GetBytes(@"{ ""type"": ""demo"", ""name"": ""asdfasdf"" }"); s.Write(data, 0, data.Length); } using (var res = (HttpWebResponse)req.GetResponse()) { Console.WriteLine("{0} {1}", res.StatusCode, res.ContentType); //Console.WriteLine("{0} bytes:", res.ContentLength); using (var s = new StreamReader(res.GetResponseStream())) Console.WriteLine(s.ReadToEnd()); } 

    You have almost the same thing, right? There is not much difference in doing this async and in this case Webclient gives exactly the same query as yours.

    In real applications, of course, the string is formed using Json.Net from a populated class with data.

    In PHP, process it like this:

     $handler->Execute(file_get_contents("php://input")); 

    And in Execute, something like:

     function Execute($postdata) { try{ $this->request = json_decode($postdata, true); 

    The variables type and name will be visible as $this->request['type'] and $this->request['name']

    I have such a bunch of work for a long time, though without base64 (there was no need), how much is this part of the question needed? I believe that when you go to php: // input the problem will resolve itself, but you can also clarify the answer.

    • Does VS tell me that HttpWebResponse does not contain a GetRequestStream? There is a replacement option to GetRequestStreamAsync but then he writes: "the type used in the using operator must have an implicit conversion in System.IDisposable ... - Denisok
    • And the difficulty with base64 is that when you transfer files, a very long string is thus formed. And not any way to allow such perversion. - Denisok
    • I tried to change only the PHP code so far, but the server still does not see my requests ... ( - Denisok