跳到主要内容
版本:1.3.9

创建HttpClient

说明

HttpClient是Http客户端类。主要用于请求Http报文。

可配置项

继承TcpClient

支持插件接口

支持ITcpPlugin接口。

创建HttpClient

HttpClient client = new HttpClient();

client.Setup(new TouchSocketConfig()
.UsePlugin()
.SetRemoteIPHost(new IPHost("http://localhost:7219")))
.Connect();//先做连接

HttpRequest request = new HttpRequest();
request
.InitHeaders()
.SetUrl("/WeatherForecast")
.SetHost(client.RemoteIPHost.Host)
.AsGet();

var respose = client.Request(request, timeout: 1000*10);
Console.WriteLine(respose.GetBody());

创建HttpsClient

如果需要连接到Https服务器。则必须手动配置Ssl。示例如下:

如果服务器证书是由证书机构颁发,则只需填充TargetHostSslProtocols

SetClientSslOption(new ClientSslOption() { TargetHost = "localhost", SslProtocols = SslProtocols.Tls12 })

如果服务器证书是由自己制作的,则需要加载证书文件,和必要的验证

.SetClientSslOption(new ClientSslOption()
{
ClientCertificates = new X509CertificateCollection() { new X509Certificate2("RRQMSocket.pfx", "RRQMSocket") },
SslProtocols = SslProtocols.Tls12,
TargetHost = "127.0.0.1",
CertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { return true; }
}));

主要方法简介

方法名功能简介
Request请求Http数据,并等待返回