"Si un trabajador quiere hacer bien su trabajo, primero debe afilar sus herramientas." - Confucio, "Las Analectas de Confucio. Lu Linggong"
Página delantera > Programación > ¿Cómo enviar solicitudes de publicación HTTP con datos del cuerpo en .NET?

¿Cómo enviar solicitudes de publicación HTTP con datos del cuerpo en .NET?

Publicado el 2025-03-05
Navegar:312

Explicación detallada del método de solicitud de publicación HTTP que contiene datos de cuerpo en.net

Este artículo presenta varios métodos para enviar solicitudes de publicación HTTP en .NET y pasar los datos del cuerpo.

How to Send HTTP POST Requests with Body Data in .NET?

1. httpClient es el método de solicitud HTTP preferido para .NET Core y versiones posteriores de .NET Framework. Proporciona operaciones asincrónicas y de alto rendimiento.

usando System.net.http; var client = new httpClient (); valores var = new Dictionary { {"Thing1", "Hola"}, {"Thing2", "World"} }; VAR content = new FormUrlEncodedContent (valores); VAR Respuesta = Await Client.Postasync ("http://www.example.com/receptile.aspx", content);
using System.Net.Http;
var client = new HttpClient();
var values = new Dictionary
{
    { "thing1", "hello" },
    { "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
2.

reestsharp:

]

usando RestSharp; var client = new RestClient ("http://example.com"); Var request = new RestRequest ("Resource/{id}"); request.AddParameter ("Thing1", "Hola"); request.AddParameter ("Thing2", "World"); VAR Respuesta = Client.Post (request);

using RestSharp;
var client = new RestClient("http://example.com");
var request = new RestRequest("resource/{id}");
request.AddParameter("thing1", "Hello");
request.AddParameter("thing2", "world");
var response = client.Post(request);
]]

usando flurl.http; VAR ResponseRing = APERAT "http://www.example.com/receptile.aspx" .PosturlencodeSync (new {Thing1 = "Hello", Thing2 = "World"}) .RECeivestring ();

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

usando System.net; usando System.Text; Var request = (httpwebRequest) webRequest.create ("http://www.example.com/receptile.aspx"); var postdata = "thing1 =" uri.escapedataString ("hola"); postdata = "& thing2 =" uri.AscapedataString ("mundo"); var data = encoding.ascii.getbytes (postdata); usando (var stream = request.getRequestStream ()) {stream.write (data, 0, data.length); Var respuesta = request.getResponse ();

CONSEGUIR:
using System.Net;
using System.Text;
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);
using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); }
var response = request.GetResponse();
var request = (httpwebrequest) webRequest.create ("http://www.example.com/receptile.aspx"); Var respuesta = request.getResponse ();

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

usando System.net; usando System.Collections.pecialized; usando (var client = new WebClient ()) { valores var = new NameValueCollection (); valores ["Thing1"] = "Hello"; valores ["Thing2"] = "World"; Respuesta var = Client.uploAdValues ​​("http://www.example.com/receptile.aspx", valores); }

CONSEGUIR:

using System.Net;
using System.Collections.Specialized;
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);
}
Último tutorial Más>

Descargo de responsabilidad: Todos los recursos proporcionados provienen en parte de Internet. Si existe alguna infracción de sus derechos de autor u otros derechos e intereses, explique los motivos detallados y proporcione pruebas de los derechos de autor o derechos e intereses y luego envíelos al correo electrónico: [email protected]. Lo manejaremos por usted lo antes posible.

Copyright© 2022 湘ICP备2022001581号-3