Implementation of the visual studio post



  • Realization took an example, but still mistakes, perhaps because the example for UWP would not help the kettle... If I remove the request.ContentLength and close the flows. Is something going to change to UWP? It's just that I've decided that all the streams in UWP are shutting down. введите сюда описание изображения

    There's a code of some other example, but the point is the same. Studio, as it did at the 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 ();
    

    Next, I've removed some of them, and some of them have slightly changed, there's no mistake, and I'm even working on it.

    // 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));
            }
    


  • Why is this WebRequest thing? This is a working example. You can use and try-catch and you can ski.

    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;
    }
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2