Overview
This documentation provides a comprehensive guide to the C# System.Net.HttpWebrequest class, the core class for handling HTTP requests in .NET.
Class Properties
- System.Net.HttpWebrequest.ClientFactory: Responsible for creating and configuring HTTP clients.
- System.Net.HttpWebrequest.Request: Represents the HTTP request being sent.
- System.Net.HttpWebrequest.Response: Represents the HTTP response received.
- System.Net.HttpWebrequest.ResponseEncoding: Handles encoding and decoding of responses.
Methods
- System.Net.HttpWebrequest.Get: Retrieves a single response.
- System.Net.HttpWebrequest.GetEnumerator: Get the response and return a Iterator.
- System.Net.HttpWebrequest.Set: Set a response.
- System.Net.HttpWebrequest.Download: Download a response.
- System.Net.HttpWebrequest.ReadAll: Read all the data from the response.
- System.Net.HttpWebrequest.Text: Convert a response to a string.
- System.Net.HttpWebrequest.Stream: Get a stream object from the response.
- System.Net.HttpWebrequest.Decode: Decode a response.
- System.Net.HttpWebrequest.ReadText: Read a response as text.
- System.Net.HttpWebrequest.SetContent: Set a response's content.
- System.Net.HttpWebrequest.Flush: Flush the response stream.
- System.Net.HttpWebrequest.Close: Close the request.
Example Code
Here's a simple example:
using System;
public class Example
{
public static void main(string[] args)
{
// Create an HTTP client
var client = new HttpClient(80);
try {
//Make request
client.Get("https://www.google.com");
//Handle the response
string response = client.GetResponse().Stream.ReadText();
Console.WriteLine(response);
}
catch(Exception e) {
Console.WriteLine(e.ToString());
}
}
}