static class · System.Net

HttpClient

public static class HttpClient

The smallest HTTP client: GET over http://host[:port]/path, one connection per request, no TLS.

Tipo
static class
Espacio de nombres
System.Net
Ensamblado
System.Core
Acceso
public, static, abstract
Archivo fuente
Net.ant

Herencia

Clases base
Any HttpClient

Métodos

Get(str url) : HttpResponse
#

static

GET of an http://host[:port][/path] URL (port 80 when none). An https:// URL is an ArgumentError: TLS is not supported; so is a URL without a host or with a port that is no number.

Miembros heredados (5)

Código fuente

HttpClient en Antril, descompilado a partir de su Larva HIR.

System.Net.HttpClient.ant
// System.Net.HttpClient — System.Core (decompiled from Larva HIR)

import System;

into namespace System.Net
{
	/// The smallest HTTP client: GET over `http://host[:port]/path`, one connection per request, no TLS.
	public static class HttpClient
	{
		public:
			/// GET of an `http://host[:port][/path]` URL (port 80 when none). An `https://` URL is an ArgumentError: TLS
			/// is not supported; so is a URL without a host or with a port that is no number.
			Get(str url) : HttpResponse
			{
				if (url.StartsWith("https://"))
					throw new ArgumentError("HttpClient: https is not supported (no TLS): " + url);
				str rest = url;
				if (rest.StartsWith("http://"))
					rest = rest.Substring(7);
				i32 slash = rest.IndexOf("/");
				str authority = slash < 0 ? rest : rest.Substring(0, slash);
				str path = slash < 0 ? "/" : rest.Substring(slash);
				i32 colon = authority.IndexOf(":");
				str host = colon < 0 ? authority : authority.Substring(0, colon);
				i32 port = 80;
				if (colon >= 0)
				{
					i32? given = Int32.TryParse(authority.Substring(colon + 1));
					if (given == null)
						throw new ArgumentError("HttpClient: bad port in the URL: " + url);
					port = given.Value;
				}
				if (host.Length == 0)
					throw new ArgumentError("HttpClient: no host in the URL: " + url);
				TcpClient client = TcpClient.Connect(host, port);
				client.Write("GET " + path + " HTTP/1.0\r\nHost: " + authority + "\r\nConnection: close\r\n\r\n");
				StringBuilder received = new StringBuilder();
				loop (true)
				{
					str piece = client.Read(4096);
					if (piece.Length == 0)
						break;
					received.Append(piece);
				}
				client.Close();
				str all = received.ToString();
				i32 split = all.IndexOf("\r\n\r\n");
				str head = split < 0 ? all : all.Substring(0, split);
				str body = split < 0 ? "" : all.Substring(split + 4);
				i32 status = 0;
				i32 space = head.IndexOf(" ");
				if (space >= 0 && head.Length >= space + 4)
					status = Int32.Parse(head.Substring(space + 1, 3));
				return new HttpResponse(status, head, body);
			}
	}
}
Esc