httpclient设置proxy与proxyselector

  1. If single proxy for all targets is enough for you:

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory 
        = new HttpComponentsClientHttpRequestFactory( HttpClientBuilder.create() .setProxy(new HttpHost("myproxy.com", 80, "http")) .build()); restTemplate = new RestTemplate(clientHttpRequestFactory);
  2. Or if you want to use different proxies for different target URIs, schemas, etc. you can useHttpRoutePlanner with custom ProxySelector:

    HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new MyProxySelector()); HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory( HttpClientBuilder.create() .setRoutePlanner(routePlanner) .build()); restTemplate = new RestTemplate(clientHttpRequestFactory);
  3. Example proxy selector: MyProxySelector.java:
  4. package hello;
    
    import java.io.IOException; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.Proxy.Type; import java.net.ProxySelector; import java.net.SocketAddress; import java.net.URI; import java.util.ArrayList; import java.util.List; public class MyProxySelector extends ProxySelector { ProxySelector defaultproxySelector = ProxySelector.getDefault(); ArrayList<Proxy> noProxy = new ArrayList<Proxy>(); ArrayList<Proxy> secureProxy = new ArrayList<Proxy>(); ArrayList<Proxy> sociaMediaProxy = new ArrayList<Proxy>(); public MyProxySelector(){ noProxy.add(Proxy.NO_PROXY); secureProxy.add(new Proxy(Type.HTTP, new InetSocketAddress( "secure.proxy.mycompany.com", 8080))); sociaMediaProxy.add(new Proxy(Type.HTTP, new InetSocketAddress( "social-media.proxy.mycompany.com", 8080))); } @Override public List<Proxy> select(URI uri) { // No proxy for local company addresses. if ( uri.getHost().toLowerCase().endsWith("mycompany.com") ) { return noProxy ; } // Special proxy for social networks. String

转载于:https://www.cnblogs.com/zhengqun/p/8302702.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient是一个用于发送HTTP请求的开源库,它提供了丰富的功能和灵活的配置选项。在使用HttpClient发送请求时,可以通过设置ContentType来指定请求的内容类型。 设置ContentType的方式取决于具体的编程语言和HttpClient库的版本。以下是一些常见的设置ContentType的方法: 1. Java语言中使用Apache HttpClient库: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; // 创建HttpClient实例 CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建HttpPost请求 HttpPost httpPost = new HttpPost("http://example.com/api"); // 设置请求体内容 StringEntity requestEntity = new StringEntity("request body", ContentType.APPLICATION_JSON); httpPost.setEntity(requestEntity); // 设置请求头中的ContentType httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); // 发送请求并获取响应 CloseableHttpResponse response = httpClient.execute(httpPost); ``` 2. Python语言中使用requests库: ```python import requests # 发送POST请求,设置请求体内容和ContentType response = requests.post('http://example.com/api', json={"key": "value"}, headers={"Content-Type": "application/json"}) ``` 3. C#语言中使用HttpClient类: ```csharp using System.Net.Http; using System.Net.Http.Headers; // 创建HttpClient实例 HttpClient httpClient = new HttpClient(); // 设置请求体内容和ContentType HttpContent content = new StringContent("request body"); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // 发送POST请求 HttpResponseMessage response = await httpClient.PostAsync("http://example.com/api", content); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值