---
id: createhttpclient
title: 创建HttpClient
---

## 说明

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


## 可配置项

继承TcpClient

<a name="mMhq1"></a>

## 支持插件接口

支持**ITcpPlugin**接口。

<a name="LAsiE"></a>

## 创建HttpClient

```csharp
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());
```

<a name="TixmX"></a>

## 创建HttpsClient

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

如果服务器证书是由**证书机构颁发**，则只需填充**TargetHost**和**SslProtocols**。

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

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

```csharp
.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; }
}));
```

<a name="mhUPx"></a>

## 主要方法简介

| **方法名** | **功能简介** |
| --- | --- |
| Request | 请求Http数据，并等待返回 |

<a name="loqXr"></a>

##
