• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #[cfg(feature = "tokio_base")]
14 use ylong_http_client::async_impl::{Client, Downloader};
15 #[cfg(feature = "tokio_base")]
16 use ylong_http_client::util::Redirect;
17 #[cfg(feature = "tokio_base")]
18 use ylong_http_client::{Certificate, HttpClientError, Request, TlsVersion};
19 #[cfg(feature = "tokio_base")]
main()20 fn main() {
21     let rt = tokio::runtime::Builder::new_multi_thread()
22         .enable_all()
23         .build()
24         .expect("Tokio runtime build err.");
25     let mut v = vec![];
26     for _i in 0..3 {
27         let handle = rt.spawn(req());
28         v.push(handle);
29     }
30 
31     rt.block_on(async move {
32         for h in v {
33             let _ = h.await;
34         }
35     });
36 }
37 
req() -> Result<(), HttpClientError>38 async fn req() -> Result<(), HttpClientError> {
39     let v = "some certs".as_bytes();
40     let cert = Certificate::from_pem(v)?;
41 
42     // Creates a `async_impl::Client`
43     let client = Client::builder()
44         .redirect(Redirect::default())
45         .tls_built_in_root_certs(false) // not use root certs
46         .danger_accept_invalid_certs(true) // not verify certs
47         .max_tls_version(TlsVersion::TLS_1_2)
48         .min_tls_version(TlsVersion::TLS_1_2)
49         .add_root_certificate(cert)
50         .build()?;
51 
52     // Creates a `Request`.
53     let request = Request::get("https://www.baidu.com")
54         .body("".as_bytes())
55         .map_err(|e| HttpClientError::other(Some(e)))?;
56 
57     // Sends request and receives a `Response`.
58     let response = client.request(request).await?;
59 
60     println!("{}", response.status().as_u16());
61     println!("{}", response.headers());
62 
63     // Reads the body of `Response` by using `BodyReader`.
64     let _ = Downloader::console(response).download().await;
65     Ok(())
66 }
67