Каноническая
Как я могу сделать HTTP-запрос и отправить некоторые данные, используя методPOST
**?
Я могу сделать GET
запрос, но не знаю, как сделать POST
.
Существует несколько способов выполнения HTTP GET
и POST
запросов:
Это обертка вокруг HttpWebRequest
. Сравните с WebClient
.
Доступен в: .NET Framework 4.5+
, .NET Standard 1.1+
, .NET Core 1.0+
.
В настоящее время предпочтительный подход. Асинхронный. Портативная версия для других платформ доступна через NuGet.
using System.Net.Http;
Рекомендуется рекомендуется создать один HttpClient
на все время работы приложения и использовать его совместно.
private static readonly HttpClient client = new HttpClient();
Смотрите HttpClientFactory
для решения проблемы инжекции зависимостей.
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");
Проверенная и протестированная библиотека для взаимодействия с REST API. Портативная. Доступна через NuGet.
Более новая библиотека, имеющая гибкий API и вспомогательные средства тестирования. HttpClient под капотом. Портативная. Доступна через NuGet.
Используя 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();
Доступен в: .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=" + Uri.EscapeDataString("hello");
postData += "&thing2=" + Uri.EscapeDataString("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();
Это обертка вокруг HttpWebRequest
. Сравните с HttpClient
.
Доступен в: .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");
}
Простой 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);
}
В 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();
}
}
}
Это полностью рабочий пример отправки/получения данных в формате JSON, я использовал VS2013, Экспресс-выпуск
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
}
public class Program
{
private static readonly HttpClient _Client = new HttpClient();
private static JavaScriptSerializer _Serializer = new JavaScriptSerializer();
static void Main(string[] args)
{
Run().Wait();
}
static async Task Run()
{
string url = "http://www.example.com/api/Customer";
Customer cust = new Customer() { Name = "Example Customer", Address = "Some example address", Phone = "Some phone number" };
var json = _Serializer.Serialize(cust);
var response = await Request(HttpMethod.Post, url, json, new Dictionary<string, string>());
string responseText = await response.Content.ReadAsStringAsync();
List<YourCustomClassModel> serializedResult = _Serializer.Deserialize<List<YourCustomClassModel>>(responseText);
Console.WriteLine(responseText);
Console.ReadLine();
}
/// <summary>
/// Makes an async HTTP Request
/// </summary>
/// <param name="pMethod">Those methods you know: GET, POST, HEAD, etc...</param>
/// <param name="pUrl">Very predictable...</param>
/// <param name="pJsonContent">String data to POST on the server</param>
/// <param name="pHeaders">If you use some kind of Authorization you should use this</param>
/// <returns></returns>
static async Task<HttpResponseMessage> Request(HttpMethod pMethod, string pUrl, string pJsonContent, Dictionary<string, string> pHeaders)
{
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Method = pMethod;
httpRequestMessage.RequestUri = new Uri(pUrl);
foreach (var head in pHeaders)
{
httpRequestMessage.Headers.Add(head.Key, head.Value);
}
switch (pMethod.Method)
{
case "POST":
HttpContent httpContent = new StringContent(pJsonContent, Encoding.UTF8, "application/json");
httpRequestMessage.Content = httpContent;
break;
}
return await _Client.SendAsync(httpRequestMessage);
}
}
}
Есть некоторые действительно хорошие ответы здесь. Дайте мне другой способ, чтобы установить заголовки с веб-клиента(). Я также покажу вам, как установить ключ API.
var client = new WebClient();
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + passWord));
client.Headers[HttpRequestHeader.Authorization] = $"Basic {credentials}";
//If you have your data stored in an object serialize it into json to pass to the webclient with Newtonsoft's JsonConvert
var encodedJson = JsonConvert.SerializeObject(newAccount);
client.Headers.Add($"x-api-key:{ApiKey}");
client.Headers.Add("Content-Type:application/json");
try
{
var response = client.UploadString($"{apiurl}", encodedJson);
//if you have a model to deserialize the json into Newtonsoft will help bind the data to the model, this is an extremely useful trick for GET calls when you have a lot of data, you can strongly type a model and dump it into an instance of that class.
Response response1 = JsonConvert.DeserializeObject<Response>(response);
Простой (один-лайнер, нет проверки ошибок, не ждать ответа) решение Я'вэ нашел до сих пор
(new WebClient()).UploadStringAsync(new Uri(Address), dataString);
используйте с осторожностью!
Это решение не использует ничего, кроме стандартных .Внутрисетевые звонки.
Проверено:
Ссылка:
// Add a Reference to the assembly System.Web
Код:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
private async Task<WebResponse> CallUri(string url, TimeSpan timeout)
{
var uri = new Uri(url);
NameValueCollection rawParameters = HttpUtility.ParseQueryString(uri.Query);
var parameters = new Dictionary<string, string>();
foreach (string p in rawParameters.Keys)
{
parameters[p] = rawParameters[p];
}
var client = new HttpClient { Timeout = timeout };
HttpResponseMessage response;
if (parameters.Count == 0)
{
response = await client.GetAsync(url);
}
else
{
var content = new FormUrlEncodedContent(parameters);
string urlMinusParameters = uri.OriginalString.Split('?')[0]; // Parameters always follow the '?' symbol.
response = await client.PostAsync(urlMinusParameters, content);
}
var responseString = await response.Content.ReadAsStringAsync();
return new WebResponse(response.StatusCode, responseString);
}
private class WebResponse
{
public WebResponse(HttpStatusCode httpStatusCode, string response)
{
this.HttpStatusCode = httpStatusCode;
this.Response = response;
}
public HttpStatusCode HttpStatusCode { get; }
public string Response { get; }
}
Для вызова без параметров (используется на "сделать" и за кулисами):
var timeout = TimeSpan.FromSeconds(300);
WebResponse response = await this.CallUri("http://www.google.com/", timeout);
if (response.HttpStatusCode == HttpStatusCode.OK)
{
Console.Write(response.Response); // Print HTML.
}
Чтобы позвонить с параметрами (используется на "Пост" за кадром):
var timeout = TimeSpan.FromSeconds(300);
WebResponse response = await this.CallUri("http://example.com/path/to/page?name=ferret&color=purple", timeout);
if (response.HttpStatusCode == HttpStatusCode.OK)
{
Console.Write(response.Response); // Print HTML.
}
Вы можете использовать IEnterprise.Простой протокол HTTP, так как он имеет встроенный парсинг класса и построения запросов:
в
await new RequestBuilder<ExampleObject>()
.SetHost("https://httpbin.org")
.SetContentType(ContentType.Application_Json)
.SetType(RequestType.Post)
.SetModelToSerialize(dto)
.Build()
.Execute();
в
Я'м автор библиотеки, поэтому не стесняйтесь задавать вопросы или проверить код в на GitHub
Если вам нравится свободно API, вы можете использовать крошечный.RestClient. Это's доступный в версию
var client = new TinyRestClient(new HttpClient(), "http://MyAPI.com/api");
// POST
var city = new City() { Name = "Paris", Country = "France" };
// With content
var response = await client.PostRequest("City", city)
.ExecuteAsync<bool>();
Надеюсь, что помогает!
Почему это не совсем тривиально ? делаешь запрос не является и тем более не борются с последствиями, и кажется, что есть некоторые .Объем ошибок, а также - см. https://social.msdn.microsoft.com/Forums/en-US/d8d87789-0ac9-4294-84a0-91c9fa27e353/bug-in-httpclientgetasync-should-throw-webexception-not-taskcanceledexception?forum=netfxnetcom
Я в конечном итоге с помощью этого кода:
`` задача статических асинхронных<(успех боол, WebExceptionStatus WebExceptionStatus, HttpStatusCode? HttpStatusCode, строки ResponseAsString)> HttpRequestAsync(с помощью HttpClient HttpClient, который, url-строки, строки postBuffer = нуль, CancellationTokenSource карат = значение null) { попробовать { HttpResponseMessage респ = нуль;
если (postBuffer имеет значение null) { респ = CTS имеет значение null ? ждут с помощью HttpClient.С getasync(адрес) : ждут с помощью HttpClient.С getasync(адрес, ХТС.Токен);
} еще { используя (ВАР httpContent = новый StringContent(postBuffer)) { респ = CTS имеет значение null ? ждут с помощью HttpClient.PostAsync(адрес, httpContent) : ждут с помощью HttpClient.PostAsync(адрес, httpContent, ХТС.Токен); } }
ВАР respString = ждут респ.Содержание.ReadAsStringAsync(); возвращение (респ.IsSuccessStatusCode, WebExceptionStatus.Успех, респ.Состояния, respString);
} поймать (веб-исключение экс) { Статус WebExceptionStatus = ех.Статус; если (состояние == WebExceptionStatus.Ошибка протокола) { // Получаем класс httpwebresponse, так что вы можете проверить код состояния HTTP. используя (класс httpwebresponse объект HttpResponse = (класс httpwebresponse)экс.Ответ) { возвращение (ложные, статус, объект HttpResponse.Состояния, объект HttpResponse.StatusDescription); } } еще { возврат (ложь, состоянии, нулевой, экс.Метод toString()); }
} поймать (TaskCanceledException экс) { если (бывш.CancellationToken == ХТС.Маркер) { // реальной отмены, вызванного абонента возврат (ложь, WebExceptionStatus.RequestCanceled, нуль, экс.Метод toString()); } еще { // веб-запрос тайм-аут (возможно, других вещей!?) возврат (ложь, WebExceptionStatus.Тайм-аут, нуль, экс.Метод toString()); }
} поймать (исключение бывший) { возврат (ложь, WebExceptionStatus.UnknownError, нуль, экс.Метод toString()); } } ``
Это GET или POST зависит, если postBuffer
имеет значение null или нет
если успех-это истинный ответ тогда будет в ResponseAsString`
если успех-это ложь вы можете проверить WebExceptionStatus,
HttpStatusCode " и " ResponseAsString`, чтобы попытаться увидеть, что пошло не так.
Я приветствую комментарии по этому как я до сих пор не уверен, что это охватывает все случаи.