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 //! This is a simple synchronous HTTPS client example.
15 
16 use ylong_http_client::sync_impl::Client;
17 use ylong_http_client::{Certificate, HttpClientError, Redirect, Request, TlsVersion};
18 
main()19 fn main() {
20     let mut v = vec![];
21     for _i in 0..3 {
22         let handle = std::thread::spawn(|| req);
23         v.push(handle);
24     }
25 
26     for h in v {
27         let _ = h.join();
28     }
29 }
30 
req() -> Result<(), HttpClientError>31 fn req() -> Result<(), HttpClientError> {
32     let v = "some certs".as_bytes();
33     let cert = Certificate::from_pem(v)?;
34 
35     // Creates a `async_impl::Client`
36     let client = Client::builder()
37         .redirect(Redirect::default())
38         .tls_built_in_root_certs(false) // not use root certs
39         .danger_accept_invalid_certs(true) // not verify certs
40         .max_tls_version(TlsVersion::TLS_1_2)
41         .min_tls_version(TlsVersion::TLS_1_2)
42         .add_root_certificate(cert)
43         .build()?;
44 
45     // Creates a `Request`.
46     let request = Request::get("https://www.baidu.com")
47         .body("".as_bytes())
48         .map_err(HttpClientError::other)?;
49 
50     // Sends request and receives a `Response`.
51     let response = client.request(request)?;
52 
53     println!("{}", response.status().as_u16());
54     println!("{}", response.headers());
55     Ok(())
56 }
57