Πώς να κάνετε αίτημα ιστού HTTP POST

Κανονικά <br&gt, Πώς μπορώ να κάνω ένα αίτημα HTTP και να στείλω κάποια δεδομένα χρησιμοποιώντας τη μέθοδο POST; **

Μπορώ να κάνω αίτηση GET, αλλά δεν έχω ιδέα πώς να κάνω αίτηση POST.

Λύση

Υπάρχουν διάφοροι τρόποι εκτέλεσης αιτήσεων HTTP GET και POST:


Μέθοδος A: HttpClient (Προτιμώμενη μέθοδος)

Πρόκειται για ένα περιτύλιγμα γύρω από το HttpWebRequest. [Συγκρίνετε με το WebClient][1].

Διαθέσιμο σε: NET Framework 4.5+,.NET Standard 1.1+,.NET Core 1.0+` .

Επί του παρόντος η προτιμώμενη προσέγγιση. Ασύγχρονη. Φορητή έκδοση για άλλες πλατφόρμες διαθέσιμη μέσω NuGet.

using System.Net.Http;

Εγκατάσταση

Συνιστάται [συνιστάται][3] να ενσαρκώνετε έναν HttpClient για όλη τη διάρκεια ζωής της εφαρμογής σας και να τον μοιράζεστε.

private static readonly HttpClient client = new HttpClient();

Ανατρέξτε στο [HttpClientFactory][2] για μια λύση Dependency Injection.


  • POST

      var values = new Dictionary
Σχόλια (35)

Απλό αίτημα GET

using System.Net;

...

using (var wb = new WebClient())
{
    var response = wb.DownloadString(url);
}

Απλό αίτημα POST

using System.Net;
using System.Collections.Specialized;

...

using (var wb = new WebClient())
{
    var data = new NameValueCollection();
    data["username"] = "myUser";
    data["password"] = "myPassword";

    var response = wb.UploadValues(url, "POST", data);
    string responseInString = Encoding.UTF8.GetString(response);
}
Σχόλια (12)

MSDN έχει ένα δείγμα.

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();
        }
    }
}
Σχόλια (1)