.NET Framework
HTTP-Clients
Suche…
Bemerkungen
Die aktuell relevanten HTTP / 1.1-RFCs sind:
- 7230: Nachrichtensyntax und Routing
- 7231: Semantik und Inhalt
- 7232: Bedingte Anfragen
- 7233: Bereichsanfragen
- 7234: Zwischenspeicherung
- 7235: Authentifizierung
- 7239: Weitergeleitete HTTP-Erweiterung
- 7240: Header für HTTP vorziehen
Es gibt auch die folgenden Informations-RFCs:
Und der experimentelle RFC:
Verwandte Protokolle:
Lesen der GET-Antwort als Zeichenfolge mit System.Net.HttpWebRequest
string requestUri = "http://www.example.com";
string responseData;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(parameters.Uri);
WebResponse response = request.GetResponse();
using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
{
responseData = responseReader.ReadToEnd();
}
GET-Antwort wird als Zeichenfolge mit System.Net.WebClient gelesen
string requestUri = "http://www.example.com";
string responseData;
using (var client = new WebClient())
{
responseData = client.DownloadString(requestUri);
}
GET-Antwort wird als Zeichenfolge mit System.Net.HttpClient gelesen
HttpClient
ist über NuGet verfügbar : Microsoft HTTP-Clientbibliotheken .
string requestUri = "http://www.example.com";
string responseData;
using (var client = new HttpClient())
{
using(var response = client.GetAsync(requestUri).Result)
{
response.EnsureSuccessStatusCode();
responseData = response.Content.ReadAsStringAsync().Result;
}
}
Senden einer POST-Anforderung mit einer String-Nutzlast mithilfe von System.Net.HttpWebRequest
string requestUri = "http://www.example.com";
string requestBodyString = "Request body string.";
string contentType = "text/plain";
string requestMethod = "POST";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri)
{
Method = requestMethod,
ContentType = contentType,
};
byte[] bytes = Encoding.UTF8.GetBytes(requestBodyString);
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Senden einer POST-Anforderung mit einer String-Nutzlast mithilfe von System.Net.WebClient
string requestUri = "http://www.example.com";
string requestBodyString = "Request body string.";
string contentType = "text/plain";
string requestMethod = "POST";
byte[] responseBody;
byte[] requestBodyBytes = Encoding.UTF8.GetBytes(requestBodyString);
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = contentType;
responseBody = client.UploadData(requestUri, requestMethod, requestBodyBytes);
}
Senden einer POST-Anforderung mit einer String-Nutzlast mithilfe von System.Net.HttpClient
HttpClient
ist über NuGet verfügbar : Microsoft HTTP-Clientbibliotheken .
string requestUri = "http://www.example.com";
string requestBodyString = "Request body string.";
string contentType = "text/plain";
string requestMethod = "POST";
var request = new HttpRequestMessage
{
RequestUri = requestUri,
Method = requestMethod,
};
byte[] requestBodyBytes = Encoding.UTF8.GetBytes(requestBodyString);
request.Content = new ByteArrayContent(requestBodyBytes);
request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
HttpResponseMessage result = client.SendAsync(request).Result;
result.EnsureSuccessStatusCode();
Grundlegender HTTP-Downloader mit System.Net.Http.HttpClient
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
class HttpGet
{
private static async Task DownloadAsync(string fromUrl, string toFile)
{
using (var fileStream = File.OpenWrite(toFile))
{
using (var httpClient = new HttpClient())
{
Console.WriteLine("Connecting...");
using (var networkStream = await httpClient.GetStreamAsync(fromUrl))
{
Console.WriteLine("Downloading...");
await networkStream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
}
}
}
static void Main(string[] args)
{
try
{
Run(args).Wait();
}
catch (Exception ex)
{
if (ex is AggregateException)
ex = ((AggregateException)ex).Flatten().InnerExceptions.First();
Console.WriteLine("--- Error: " +
(ex.InnerException?.Message ?? ex.Message));
}
}
static async Task Run(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Basic HTTP downloader");
Console.WriteLine();
Console.WriteLine("Usage: httpget <url>[<:port>] <file>");
return;
}
await DownloadAsync(fromUrl: args[0], toFile: args[1]);
Console.WriteLine("Done!");
}
}
Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow