SORU
25 EKİM 2010, PAZARTESİ


Yazı ile HTTP isteği

Nasıl bir HTTP isteği yapmak ve bazı veriler POST yöntemi kullanarak gönderebilir miyim? GET istek yapabilirim ama POST nasıl yapacağım konusunda hiçbir fikrim yok.

CEVAP
25 EKİM 2010, PAZARTESİ


Gerçekleştir ve istekleri göndermek için çeşitli yollar vardır:


Yöntem 1: Mirası

using System.Net;

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();

OLSUN

var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");

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

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

Yöntem 2: Professional (Service (şimdi eski)

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

OLSUN

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

Yöntem 3: HttpClient

Şu anda tercih edilen bir yaklaşım. Zaman uyumsuz. Gemi ile .NuGet) NET 4.5, diğer platformlar için taşınabilir sürümü mevcut.

using System.Net.Http;

POST

using (var client = new HttpClient())
{
    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();
}

OLSUN

using (var client = new HttpClient())
{
    var responseString = client.GetStringAsync("http://www.example.com/recepticle.aspx");
}

Yöntem 4: 3. Parti Kütüphaneler

RestSharp

Denedim ve REST API ile etkileşim için kütüphane test. Taşınabilir. NuGet ile kullanılabilir.

Flurl.Http

Yeni kütüphane akıcı bir API ve test yapanlar spor. Bu başlık altında HttpClient. Taşınabilir. NuGet ile kullanılabilir.

using Flurl.Http;

POST

var responseString = await "http://www.example.com/recepticle.aspx"
    .PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })
    .ReceiveString();

OLSUN

var responseString = await "http://www.example.com/recepticle.aspx"
    .GetStringAsync();

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Kiddyzuzaa

    Kiddyzuzaa

    25 ŞUBAT 2014
  • MagmaRhino

    MagmaRhino

    16 Temmuz 2011
  • Official Clouds

    Official Clo

    1 HAZİRAN 2011