I got a System error in C# post


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.





Have a look at the inner exception
– TheGeneral
Jun 30 at 10:17





Look at this link - stackoverflow.com/questions/33235131/…
– Kaushik Srinath
Jun 30 at 10:25





@Kaushik Srinath Thank you!!! I never consider this problem.......
– LIFUGUAN
2 days ago




2 Answers
2



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.


HttpClient


Windows.Web.Http



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.





Thank you!I will try it.
– LIFUGUAN
2 days ago





It dosen't work. When I execute the code ``
– LIFUGUAN
yesterday






what exception throw ?
– Nico Zhu - MSFT
yesterday





When I execute the code httpResponse = await client.PostAsync(requestUri, content);, it throw an exception: A connection with the server could not be established. And the res is : {"status":-1,"msg":"Error! Invalid Request."}
– LIFUGUAN
yesterday


httpResponse = await client.PostAsync(requestUri, content);





Will it be a problem with the webpage?
– LIFUGUAN
yesterday



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 .....


{"status":-1,"msg":"Error! Invalid Request."}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter