HttpClient4.5.6设置代理以及代理验证(用户名和密码)

https://blog.csdn.net/manong_123/article/details/82531004

 

HttpClient4.5.6设置代理以及代理验证(用户名和密码)
下载 httpcomponents-client-4.5.6-bin 在Apache官网有,贴上链接:http://hc.apache.org/downloads.cgi 下载以后把对应的包导入工程即可(java和maven工程都行),下面说明如何设置代理验证:

import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.jupiter.api.Test;

public class DownloadPage
{
    @Test
    public void downLoad()
    {
        try
        {
            // 设置代理HttpHost
            HttpHost proxy = new HttpHost("openproxy.huawei.com", 8080, "http");

            // 设置要访问的HttpHost,即是目标站点的HttpHost
            HttpHost target = new HttpHost("baidu.com", 80);

            // 设置认证
            CredentialsProvider provider = new BasicCredentialsProvider();

            provider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("username", "password"));

            CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();

            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();

            HttpGet httpGet = new HttpGet("/");

            httpGet.setConfig(config);

            CloseableHttpResponse resp = httpClient.execute(target, httpGet);

            if (resp.getStatusLine().getStatusCode() == 200)
            {
                System.out.println("OK");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}



在执行请求时,请求设置用代理进行访问,但是代理需要验证,那么就会找到provider设置相关联的用户名和密码进行验证,所以设置provider时需要注意关联。说明一下setCredentials()这个方法可以添加多个验证账号和密码,看源代码可知就是ConcurrentHashMap(Map)的操作。
 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 HttpClient代理服务并传递参数以及获取响应结果的代码示例如下: ```java import java.io.IOException; import.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.util.ArrayList; import java.util.List; public class HttpClientExample { public static void main(String[] args) { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); try { // GET 请求示例 String getEndpoint = "https://api.example.com/endpoint"; String getRequestParams = "?param1=value1&param2=value2"; String getUrl = getEndpoint + getRequestParams; HttpGet httpGet = new HttpGet(getUrl); HttpResponse getResponse = httpClient.execute(httpGet); HttpEntity getEntity = getResponse.getEntity(); if (getEntity != null) { InputStream inputStream = getEntity.getContent(); // 处理输入流,获取响应结果 String responseString = convertInputStreamToString(inputStream); System.out.println("GET 响应结果:" + responseString); } // POST 请求示例 String postEndpoint = "https://api.example.com/endpoint"; HttpPost httpPost = new HttpPost(postEndpoint); // 设置 POST 请求参数 List<NameValuePair> postParams = new ArrayList<>(); postParams.add(new BasicNameValuePair("param1", "value1")); postParams.add(new BasicNameValuePair("param2", "value2")); httpPost.setEntity(new UrlEncodedFormEntity(postParams)); HttpResponse postResponse = httpClient.execute(httpPost); HttpEntity postEntity = postResponse.getEntity(); if (postEntity != null) { InputStream inputStream = postEntity.getContent(); // 处理输入流,获取响应结果 String responseString = convertInputStreamToString(inputStream); System.out.println("POST 响应结果:" + responseString); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } private static String convertInputStreamToString(InputStream inputStream) throws IOException { StringBuilder result = new StringBuilder(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.append(new String(buffer, 0, length, StandardCharsets.UTF_8)); } return result.toString(); } } ``` 上述代码使用 Apache HttpClient 来发送 GET 和 POST 请求。你可以根据实际情况进行修改和调整,以适应你的代理服务和传递的参数。获取到响应结果后,你可以根据需要进行进一步处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值