用python 和java 分别写一段代理IP访问的网站的例子

import requests

# 设置代理IP
proxies = {
    "http": "http://your-proxy-ip:your-proxy-port",
    "https": "http://your-proxy-ip:your-proxy-port"
}

# 访问网站
url = "http://www.example.com"
response = requests.get(url, proxies=proxies)

# 输出网页内容
print(response.text)

以上是python的

======================================================================

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpClientWithProxy {

    public static void main(String[] args) throws IOException, URISyntaxException {
        // 创建HttpClientBuilder对象
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        // 设置代理IP和端口号
        InetSocketAddress proxyAddress = new InetSocketAddress("your-proxy-ip", your-proxy-port);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);

        // 设置HttpClient的请求参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000) // 设置连接超时时间
                .setConnectionRequestTimeout(1000) // 设置从连接池获取连接的超时时间
                .setSocketTimeout(5000) // 设置响应超时时间
                .setProxy(new HttpHost(proxyAddress)) // 设置代理IP和端口号
                .build();

        // 创建CloseableHttpClient对象
        CloseableHttpClient httpClient = httpClientBuilder
                .setDefaultRequestConfig(requestConfig)
                .setProxy(proxy)
                .build();

        // 创建URI对象
        URI uri = new URIBuilder()
                .setScheme("http")
                .setHost("www.example.com")
                .build();

        // 创建HttpGet对象
        HttpUriRequest httpGet = new HttpGet(uri);

        // 发送HTTP请求并获取响应结果
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseBody = EntityUtils.toString(entity, StandardCharsets.UTF_8);
        ContentType contentType = ContentType.getOrDefault(entity);
        String mimeType = contentType.getMimeType();

        // 输出响应结果
        System.out.println("Response Status: " + response.getStatusLine());
        System.out.println("Response Body: " + responseBody);
        System.out.println("Content Type: " + mimeType);

        // 关闭HttpClient
        httpClient.close();
    }
}

以上是java的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值