Httpclinet设置代理请求问题

1.错误代码,会出现400错误

    public String test() {
        SimpleDateFormat timeFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        String gmtTime = timeFormat.format(new Date());
        String body = "{}";   //请求json串
        String contentType = "application/json";
        // 创建httpclient对象
        String result = null;
        String url = "请求url";
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpHost proxy = new HttpHost("代理IP", 代理端口, "HTTP");
        HttpPost httpPost = new HttpPost(url);

        try { // 参数键值对
            httpPost.setEntity(new StringEntity(body));
           httpPost.setHeader("Content-Type", contentType);
            HttpResponse response = httpClient.execute(proxy, httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
               
            } 
        } catch (Exception e) {
     
            e.printStackTrace();
        } finally {
            if (null != httpPost) {
                // 释放连接
                httpPost.releaseConnection();
            }
        }
        return result;
    }

2.经过修改后如下

public String test() {
        SimpleDateFormat timeFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        String gmtTime = timeFormat.format(new Date());
        String body = "{}";
        String contentType = "application/json";
        // 创建httpclient对象
        String result = null;
        String url = "";
        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpHost proxy = new HttpHost("代理IP地址", 代理端口, "HTTP");
        HttpPost httpPost = new HttpPost(url);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();

        try { // 参数键值对
            httpPost.setEntity(new StringEntity(body))
            httpPost.setHeader("Content-Type", contentType);
            httpPost.setConfig(config);
            HttpResponse response = httpClient.execute(proxy, httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (null != httpPost) {
                // 释放连接
                httpPost.releaseConnection();
            }
        }
        return result;
    }

或者可以使用okhttp

 public String test2() {
        Response response = null;

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("代理地址", 代理端口));
        builder.proxy(proxy);
        OkHttpClient client = builder.build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{请求json串}");
        Request request = new Request.Builder()
                .url("接口地址")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        try {
            response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response.toString();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值