说明

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

可配置项

继承TcpClient

支持插件接口

支持ITcpPlugin接口。

创建HttpClient

  1. HttpClient client = new HttpClient();
  2. client.Setup(new RRQMConfig()
  3. .UsePlugin()
  4. .SetRemoteIPHost(new IPHost("http://localhost:7219")))
  5. .Connect();
  6. HttpRequest request = new HttpRequest();
  7. request
  8. .InitHeaders()
  9. .SetUrl("/WeatherForecast")
  10. .SetHost(client.RemoteIPHost.Host)
  11. .AsGet();
  12. var respose = client.Request(request,timeout:1000000);
  13. Console.WriteLine(respose.GetBody());

创建HttpsClient

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

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

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

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

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

主要方法简介

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