How to make an HTTP POST web request

后端 未结 14 2804
有刺的猬
有刺的猬 2020-11-21 04:31

Canonical
How can I make an HTTP request and send some data using the POST method?

相关标签:
14条回答
  • 2020-11-21 05:05

    MSDN has a sample.

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace Examples.System.Net
    {
        public class WebRequestPostExample
        {
            public static void Main()
            {
                // Create a request using a URL that can receive a post. 
                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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:08

    Why is this not totally trivial? Doing the request is not and especially not dealing with the results and seems like there are some .NET bugs involved as well - see Bug in HttpClient.GetAsync should throw WebException, not TaskCanceledException

    I ended up with this code:

    static async Task<(bool Success, WebExceptionStatus WebExceptionStatus, HttpStatusCode? HttpStatusCode, string ResponseAsString)> HttpRequestAsync(HttpClient httpClient, string url, string postBuffer = null, CancellationTokenSource cts = null) {
        try {
            HttpResponseMessage resp = null;
    
            if (postBuffer is null) {
                resp = cts is null ? await httpClient.GetAsync(url) : await httpClient.GetAsync(url, cts.Token);
    
            } else {
                using (var httpContent = new StringContent(postBuffer)) {
                    resp = cts is null ? await httpClient.PostAsync(url, httpContent) : await httpClient.PostAsync(url, httpContent, cts.Token);
                }
            }
    
            var respString = await resp.Content.ReadAsStringAsync();
            return (resp.IsSuccessStatusCode, WebExceptionStatus.Success, resp.StatusCode, respString);
    
        } catch (WebException ex) {
            WebExceptionStatus status = ex.Status;
            if (status == WebExceptionStatus.ProtocolError) {
                // Get HttpWebResponse so that you can check the HTTP status code.
                using (HttpWebResponse httpResponse = (HttpWebResponse)ex.Response) {
                    return (false, status, httpResponse.StatusCode, httpResponse.StatusDescription);
                }
            } else {
                return (false, status, null, ex.ToString()); 
            }
    
        } catch (TaskCanceledException ex) {
            if (cts is object && ex.CancellationToken == cts.Token) {
                // a real cancellation, triggered by the caller
                return (false, WebExceptionStatus.RequestCanceled, null, ex.ToString());
            } else {
                // a web request timeout (possibly other things!?)
                return (false, WebExceptionStatus.Timeout, null, ex.ToString());
            }
    
        } catch (Exception ex) {
            return (false, WebExceptionStatus.UnknownError, null, ex.ToString());
        }
    }
    

    This will do a GET or POST depends if postBuffer is null or not

    if Success is true the response will then be in ResponseAsString

    if Success is false you can check WebExceptionStatus, HttpStatusCode and ResponseAsString to try to see what went wrong.

    0 讨论(0)
提交回复
热议问题