LIFUGUAN
LIFUGUAN

Reputation: 73

I got a System error in C# post

It is my first time to ask a question in here (I'm from Asia).

Platform:UWP 17632

IDE : Visual Studio 2017

Based on the reqiurement of the project, I need to post some information to a website.

I refer the answer about How to make HTTP POST web request Method A.

enter image description here

Here is my code:

public async void PostDataAsync(string pTemperture, string pHumidity, string pFireStatus, string pLightStatus, string pBodyStatus)
   {
       var values = new Dictionary<string, string>
           {
               {"count", "1" },
               {"temperture_0", pTemperture },
               {"Humidity_0", pHumidity },
               {"FireStatus_0", pFireStatus },
               {"LightStatus_0" ,pLightStatus},
               {"BodyDetect_0", pBodyStatus }
           };
       var content = new FormUrlEncodedContent(values);
       try
       {
           var response = await client.PostAsync("http://115.159.36.210/api/onehome/upload", content);//Here throw an exception
           System.Diagnostics.Debug.WriteLine(response);
           var responseString = response.Content.ReadAsStringAsync();
           System.Diagnostics.Debug.WriteLine(responseString);
       }
       catch (Exception ex)
       {
           System.Diagnostics.Debug.WriteLine(ex.HelpLink);
           System.Diagnostics.Debug.WriteLine(ex.Message);
           throw;
       }            
   }

And then it throws an exception

“An error occurred while sending the request.” 

in

var response = await client.PostAsync("http://115.159.36.210/api/onehome/upload", content);

I want to know why and gain the solution which can solve it.
I will be grateful if you can help me.

Upvotes: 2

Views: 315

Answers (2)

Eric Lian
Eric Lian

Reputation: 21

I am the author of the server.

The reality is I have not finish the code of the server.

Thus , {"status":-1,"msg":"Error! Invalid Request."} is the default result .....

Upvotes: 2

Nico Zhu
Nico Zhu

Reputation: 32785

It is recommend that use HttpClient and the rest of the Windows.Web.Http namespace API to send and receive information using the HTTP 2.0 and HTTP 1.1 protocols within UWP.

For your requirement, you could make a method to package http POST method like the follow

public async void SendPostMethod(string url, Dictionary<string, string> param, Action<string> response)
{
    HttpClient client = new HttpClient();

    HttpResponseMessage httpResponse = new HttpResponseMessage();
    Uri requestUri = new Uri(url);

    var content = new HttpFormUrlEncodedContent(param);
    try
    {
        httpResponse = await client.PostAsync(requestUri, content);

        response(await httpResponse.Content.ReadAsStringAsync());
    }
    catch (Exception ex)
    {

    }

}

Usage

 this.SendPostMethod("http://115.159.36.210/api/onehome/upload",Param, (res) =>
{

    var response = res;

});

And there are official code sample and document that you could refer.

Upvotes: 2

Related Questions