Buscar contenidos

viernes, 31 de marzo de 2017

C# Métodos POST/GET HttpWebRequest HttpClient


Link artículo




down voteaccepted
There are several ways to perform HTTP GET and POST requests:

Method A: HttpClient
Available in: .NET Framework 4.5+, .NET Standard 1.1+, .NET Core 1.0+
Currently the preferred approach. Asynchronous. Portable version for other platforms available via NuGet.
using System.Net.Http;
Setup
It is recommended to instantiate one HttpClient for your application's lifetime and share it.
private static readonly HttpClient client = new HttpClient();
POST
var values = new Dictionary<string, string>
{
   { "thing1", "hello" },
   { "thing2", "world" }
};

var content = new FormUrlEncodedContent(values);

var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

var responseString = await response.Content.ReadAsStringAsync();
GET
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");

Method B: 3rd-Party Libraries
Tried and tested library for interacting with REST APIs. Portable. Available via NuGet.
Newer library sporting a fluent API and testing helpers. HttpClient under the hood. Portable. Available via NuGet.
using Flurl.Http;
POST
var responseString = await "http://www.example.com/recepticle.aspx"
    .PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })
    .ReceiveString();
GET
var responseString = await "http://www.example.com/recepticle.aspx"
    .GetStringAsync();

Method C: Legacy
Available in: .NET Framework 1.1+, .NET Standard 2.0+, .NET Core 1.0+
using System.Net;
using System.Text;  // for class Encoding
using System.IO;    // for StreamReader
POST
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");

var postData = "thing1=hello";
    postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
GET
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Method D: WebClient (Also now legacy)
Available in: .NET Framework 1.1+, .NET Standard 2.0+, .NET Core 2.0+
using System.Net;
using System.Collections.Specialized;
POST
using (var client = new WebClient())
{
    var values = new NameValueCollection();
    values["thing1"] = "hello";
    values["thing2"] = "world";

    var response = client.UploadValues("http://www.example.com/recepticle.aspx", values);

    var responseString = Encoding.Default.GetString(response);
}
GET
using (var client = new WebClient())
{
    var responseString = client.DownloadString("http://www.example.com/recepticle.aspx");
}

No hay comentarios:

Publicar un comentario