http请求中各种编程语言设置代理的方式

各种编程语言中使用代理的方法归总


Golang设置代理的方法

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strconv"
    "time"
)
type MProxy struct {
    Ip        string
    Port     int
}
func main() {
    // 创建代理对象,此处的代理软件使用的是Charles抓包软件
    mProxy :=MProxy{
        Ip:   "127.0.0.1",
        Port: 51100,
    }
    // 创建代理链接
    proxyURL, _ := url.Parse("http://" + mProxy.Ip + ":" + strconv.Itoa(mProxy.Port))
    fmt.Println("will use" ,proxyURL,"start query")
    proxy := http.ProxyURL(proxyURL)
    // 使用代理创建transport
    transport := &http.Transport{Proxy: proxy}
    client := &http.Client{
        Transport: transport, // 设置transport
        Timeout: 30 * time.Second, // 设置超时时间
    }

    // 使用GET请求方式,请求ip归属地校验地址
    req, _ := http.NewRequest("GET", "https://ip.useragentinfo.com/json", nil)
    // 设置请求的UserAgent,User-Agent信息可参考  https://www.useragentinfo.com/ua/randomUa
    req.Header.Set("User-Agent", "Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36")

    //开始发起请求
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err,proxyURL)
    } else {
        //读取结果,并进行输出
        defer resp.Body.Close()
        data, _ := ioutil.ReadAll(resp.Body)
        //如代理可连接的话,将会输出代理ip的归属地信息
        fmt.Println(string(data))
    }
}

Python requests 模块设置代理的方法

个人比较喜欢使用requests模块。原生的urllib感觉比较繁琐。官方参考链接:requests proxies

import json
import requests

if __name__ == '__main__':
    proxy = "127.0.0.1:51100"
    ua = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
    try:
        proxies = {
            "http": "http://" + proxy,
            "https": "http://" + proxy,
        }
        r = requests.get("https://ip.useragentinfo.com/json", headers={'user-agent': ua}, proxies=proxies, timeout=2)
        myJson = json.loads(r.content.decode("utf-8"))
        print(myJson)
    except Exception as e:
        print({"origin_proxy": proxy, "desc": str(e), "code": "500"})

Python selenium 设置proxy 的方法

from selenium import webdriver  # pip3 install selenium
from selenium.webdriver.chrome.service import Service
from selenium_stealth import stealth  # pip3 install selenium_stealth
from webdriver_manager.chrome import ChromeDriverManager  # pip3 install webdriver_manager
import time

chrome_option = webdriver.ChromeOptions()
# chrome_option.add_argument('--headless') 
chrome_option.add_argument('--disable-gpu')
chrome_option.add_argument('--no-sandbox')
chrome_option.add_argument('--local-timezone=Asia/Shanghai')
chrome_option.add_argument("start-maximized")
chrome_option.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_option.add_experimental_option('useAutomationExtension', False)
chrome_option.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})

proxy = "127.0.0.1:51100"
# 此行代码是关键
webdriver.DesiredCapabilities.CHROME['proxy'] = {"httpProxy": proxy, "sslProxy": proxy,
                                                 "proxyType": "MANUAL", }

service = Service(ChromeDriverManager().install())
with webdriver.Chrome(service=service, options=chrome_option) as browser:
    stealth(browser,
            user_agent='Mozilla/5.0 (Linux; Android 10; ELS-AN00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Mobile Safari/537.36',
            languages=["zh-CN", "cn"],
            vendor="Google Inc.",
            platform="Linux armv81",
            webgl_vendor="Intel Inc.",
            renderer="",
            fix_hairline=True,
            )

    browser.get("https://ip.useragentinfo.com")
    time.sleep(60)
    browser.close()

Python urllib 模块设置代理的方法

import json
from urllib import request as client

if __name__ == '__main__':
    proxy = "localhost:7888"

    req = client.Request("https://ip.useragentinfo.com/json")
    req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
    req.set_proxy(proxy, 'https')

    resp = client.urlopen(req)
    print(json.loads(resp.read().decode("utf-8")))

Java httpclient模块设置代理的方法

Java网络请求中,httpclient有着举足轻重的地位。其官方链接为:Apache HttpComponents

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import java.util.concurrent.TimeUnit;

public class HttpClientProxy {

    public static void main(String[] args) {
        try {
            final HttpGet request = new HttpGet("https://ip.useragentinfo.com/json");
            //127.0.0.1 is proxy server ip address  51100 is proxy server ip port
            final HttpHost proxy = new HttpHost("http", "127.0.0.1", 51100);
            final RequestConfig config = RequestConfig.custom().setConnectTimeout(3, TimeUnit.SECONDS).setProxy(proxy).build();
            request.setConfig(config);
            request.setHeader("User-Agent","Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36");

            final CloseableHttpClient httpclient = HttpClients.createDefault();
            final CloseableHttpResponse response = httpclient.execute(request);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            System.out.println("query occur an exception : " + e.getMessage());
        }
    }
}

Kotlin httpclient 模块设置代理的方法

个人比较喜欢使用kotlin,现在java语言使用的比较少。java与kotlin的转换开始非常的别扭,不过习惯之后,再也不想回到java。讨论一种语言的优劣,其实最简单的办法,就是将语言学会,然后再与其他的语言进行对比。而不是上来空口白话,因为程序员的内心第一反应都是抗拒,不喜欢离开自己曾经的舒适区。只有打破思维的约束,才能接着往下走不是。

import org.apache.hc.client5.http.classic.methods.HttpGet
import org.apache.hc.client5.http.config.RequestConfig
import org.apache.hc.client5.http.impl.classic.HttpClients
import org.apache.hc.core5.http.HttpHost
import org.apache.hc.core5.http.io.entity.EntityUtils
import java.util.concurrent.TimeUnit

object HttpClientProxy {
    @JvmStatic
    fun main(args: Array) {
        try {
            val request = HttpGet("https://ip.useragentinfo.com/json")
            val proxy = HttpHost("http", "127.0.0.1", 51100)
            request.config = RequestConfig.custom().setConnectTimeout(3, TimeUnit.SECONDS).setProxy(proxy).build()
            request.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36")
            val response = HttpClients.createDefault().execute(request)
            println(EntityUtils.toString(response.entity))
        } catch (e: Exception) {
            println("query occur an exception : " + e.message)
        }
    }
}

nodejs 使用http.request设置proxy

let http = require('http');

let options = {
    // your target query url
    path: 'https://ip.useragentinfo.com/json',
    headers: {'User-Agent': 'Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36'},
    host: '127.0.0.1', // IP address of proxy server 代理服务器的ip地址
    port:51100, // port of proxy server 代理服务器的端口信息
};

http.get(options, function(res) {
    res.pipe(process.stdout);
});

Android OkHttp proxy setting method

安卓中常使用okhttp发起网络请求,那么在okhttp中如何设置代理那?不过这样用的概率偏小,一般会将设备作为代理的服务端,而不是代理的客户端。

//127.0.0.1 is proxy server ip address ,51100 is proxy server ip port
val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress("127.0.0.1", 51100))

val okHttpClient = OkHttpClient.Builder().proxy(proxy).build()
val request = Request.Builder().url(getString(R.string.task_query_url))
        .addHeader(
            "User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
        ).build()
try {
    okHttpClient.newCall(request).execute().use { response ->
        val returnValue = response.body!!.string()
        Log.i("content",returnValue)
    }
} catch (e: Exception) {
    Toast.makeText(this@MainActivity, e.message.toString(), Toast.LENGTH_LONG).show()
}

[========]
检索关键词

  • python selenium proxy
  • java httpclient proxy
  • okhttp proxy
  • golang set proxy
  • kotlin set proxy
  • Python requests proxy
  • 设置代理
end
  • 作者:kali(作者介绍)
  • 更新时间:2022-07-20 18:09
  • 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
  • 转载声明:转载站点文章,请附上原文链接
  • 翻译声明:翻译文章会不严谨,请务必附上原文链接
  • 扫描阅读:扫描二维码,手机阅读该文章