httpClient集合

httpClient

参考文档:https://hc.apache.org/httpcomponents-client-5.2.x/quickstart.html
参考文档: https://blog.csdn.net/qq_38866898/article/details/118518221
参考文档httpClientUtils:
https://zhuanlan.zhihu.com/p/476084066?utm_id=0

同步:

SimpleHttpRequest request2 = SimpleRequestBuilder.get("http://httpbin.org/get").build();
    httpclient.execute(request2, new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(SimpleHttpResponse response2) {
            latch1.countDown();
            System.out.println(request2.getRequestUri() + "->" + response2.getCode());
        }

        @Override
        public void failed(Exception ex) {
            latch1.countDown();
            System.out.println(request2.getRequestUri() + "->" + ex);
        }

        @Override
        public void cancelled() {
            latch1.countDown();
            System.out.println(request2.getRequestUri() + " cancelled");
        }

    });

异步:

ClassicHttpRequest httpPost = ClassicRequestBuilder.post("http://httpbin.org/post")
            .setEntity(new UrlEncodedFormEntity(Arrays.asList(
                    new BasicNameValuePair("username", "vip"),
                    new BasicNameValuePair("password", "secret"))))
            .build();
    httpclient.execute(httpPost, response -> {
        System.out.println(response.getCode() + " " + response.getReasonPhrase());
        final HttpEntity entity2 = response.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity2);
        return null;
    });

流畅的API

//流畅的API让用户不必处理系统的手动解除分配
//在某些情况下,以必须在存储器中缓冲响应内容为代价的资源。

Request.Get("http://targethost/homepage")
    .execute().returnContent();
Request.Post("http://targethost/login")
    .bodyForm(Form.form().add("username",  "vip").add("password",  "secret").build())
    .execute().returnContent();
    ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 HttpClient 发送 HTTP 请求时,可以通过设置 Headers 来添加自定义的请求头信息。以下是一个示例: ```csharp using System.Net.Http; var client = new HttpClient(); client.DefaultRequestHeaders.Add("User-Agent", "My User Agent"); client.DefaultRequestHeaders.Add("Accept", "application/json"); var response = await client.GetAsync("http://www.example.com"); var content = await response.Content.ReadAsStringAsync(); ``` 在这个示例中,我们首先创建了一个 `HttpClient` 实例,然后通过 `DefaultRequestHeaders` 属性获取默认的请求头集合。我们使用 `Add` 方法向集合中添加了两个请求头信息。最后,我们使用 `GetAsync` 方法发送 GET 请求,并获取响应内容。 需要注意的是,如果你需要为每个请求设置不同的请求头信息,可以在发送请求时通过 `HttpRequestMessage.Headers` 属性来设置。例如: ```csharp using System.Net.Http; var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Get, "http://www.example.com"); request.Headers.Add("User-Agent", "My User Agent"); request.Headers.Add("Accept", "application/json"); var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); ``` 在这个示例中,我们通过创建一个 `HttpRequestMessage` 实例,并在构造函数中设置请求方法和 URL。然后,我们使用 `Headers` 属性向请求头中添加了两个请求头信息。最后,我们使用 `SendAsync` 方法发送请求,并获取响应内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值