1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 use ylong_http_client::sync_impl::Client;
15 use ylong_http_client::util::Redirect;
16 use ylong_http_client::{Certificate, HttpClientError, Request, TlsVersion};
17
main()18 fn main() {
19 let mut v = vec![];
20 for _i in 0..3 {
21 let handle = std::thread::spawn(|| req);
22 v.push(handle);
23 }
24
25 for h in v {
26 let _ = h.join();
27 }
28 }
29
req() -> Result<(), HttpClientError>30 fn req() -> Result<(), HttpClientError> {
31 let v = "some certs".as_bytes();
32 let cert = Certificate::from_pem(v)?;
33
34 // Creates a `async_impl::Client`
35 let client = Client::builder()
36 .redirect(Redirect::default())
37 .tls_built_in_root_certs(false) // not use root certs
38 .danger_accept_invalid_certs(true) // not verify certs
39 .max_tls_version(TlsVersion::TLS_1_2)
40 .min_tls_version(TlsVersion::TLS_1_2)
41 .add_root_certificate(cert)
42 .build()?;
43
44 // Creates a `Request`.
45 let request = Request::get("https://www.baidu.com")
46 .body("".as_bytes())
47 .map_err(|e| HttpClientError::other(Some(e)))?;
48
49 // Sends request and receives a `Response`.
50 let response = client.request(request)?;
51
52 println!("{}", response.status().as_u16());
53 println!("{}", response.headers());
54 Ok(())
55 }
56