I took the implementation from an example but still errors, perhaps because the example is not for the UWP help the teapot ... If I remove the request. ContentLength and close the threads. What is going to come to UWP? I just decided that in the UWP all threads stop themselves. enter image description here

Here is the code from a slightly different example, but the essence is the same. Studio as at that time swears on .Close () and ContentLength

WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx "); // Set the Method property of the request to POST. request.Method = "POST"; // Create POST data and convert it to a byte array. string postData = "This is a test that posts this string to a Web server."; byte[] byteArray = Encoding.UTF8.GetBytes (postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream (); // Write the data to the request stream. dataStream.Write (byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close (); // Get the response. WebResponse response = request.GetResponse (); // Display the status. Console.WriteLine (((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream (); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader (dataStream); // Read the content. string responseFromServer = reader.ReadToEnd (); // Display the content. Console.WriteLine (responseFromServer); // Clean up the streams. reader.Close (); dataStream.Close (); response.Close (); 

Then I deleted some and changed a little, there are no errors now and it even works (please check the current code for provilnost)

 // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("Какой-нибудь мой сервак"); // Set the Method property of the request to POST. request.Method = "POST"; // Create POST data and convert it to a byte array. string postData = "action=101&login=" + Login.Text + "&pass=" + Pass.Text; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. // Get the request stream. Stream dataStream = await request.GetRequestStreamAsync(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Get the response. WebResponse response = await request.GetResponseAsync(); // Display the status. Resp.Text = ((HttpWebResponse)response).StatusDescription; // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. JArray jsonArray = JArray.Parse(responseFromServer); JToken jsonArray_Item = jsonArray.First; string Answer = jsonArray_Item.Value<string>("answer"); Resp.Text += Answer; if (Answer == "ok") { Frame.Navigate(typeof(MainPage)); } 
  • one
    Format the message: it is not good to insert a code with a picture. - AK
  • You cannot delete request.ContentLenght, closing threads is also done correctly. I do not know where you copied the example from - but your code is correct, as in MSDN msdn.microsoft.com/en-us/library/… - AK
  • stackoverflow.com/questions/35619622/ ... There is another answer on the links - right about ContentLength, read, very useful information in both posts and links to MSDN. The essence is in two words: the WebRequest class is made in UWP differently, therefore it is impossible one-to-one to copy examples from the "classic" .Net. - AK
  • @Denisok Give the code of the example with text, not with a picture - PashaPash
  • @PashaPash I attached a picture so that the error was visible, but as requested they added the code itself - Denisok

1 answer 1

And why this fuss with WebRequest? Here is a working example. Wrap in using and try-catch and you can use.

 public async Task<string> SendPostRequest(string url, string body) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); var response = await httpClient.PostAsync(url, new StringContent(body, Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); return content; } 
  • Why then do we need WebRequest? And what does - Task <string> mean?) - Denisok
  • And what this line means httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); - Denisok
  • Task <string> means that an asynchronous task is created that will return a string. The first part of the question about WebRequest I can not answer anything intelligible) httpClient.DefaultRequestHeaders.Add ("Accept", "application / json"); adds headers, tells the server that it is waiting for data in the json format in response, you can delete it if you do not need to specifically specify it. - Make Makeluv
  • Thank you) Helped) - Denisok
  • But no, another question is how to address this function. string Answer = SendPostRequest("Моя сервак", "Строка которую передаю"); - So? @Make Makeluv - Denisok