Python如何接入代码demo
发布时间 2023-09-28
#!/usr/bin/env python# -*- coding: utf-8 -*-"""使用requests请求代理服务器请求http和https网页均适用"""import requests// 代理服务器proxy_ip = "proxyIp:proxyPort";# 用户名密码认证(私密代理/独享代理)username = "username"password = "password"proxies = { "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}, "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}}# 要访问的目标网页target_url = "http://baidu.com";# 使用代理IP发送请求response = requests.get(target_url, proxies=proxies)# 获取页面内容if response.status_code == 200: print(response.text)
Node如何接入代码demo
发布时间 2023-09-28
const http = require("http"); // 引入内置http模块const url = require("url");// 要访问的目标页面const targetUrl = "http://baidu.com";const urlParsed = url.parse(targetUrl);// 代理ipconst proxyIp = "proxyIp"; // 代理服务器ipconst proxyPort = "proxyPort"; // 代理服务器host// 用户名密码认证(私密代理/独享代理)const username = "username";const password = "password";const base64 = new Buffer.from(username + ":" + password).toString("base64");const options = { host : proxyIp, port : proxyPort, path : targetUrl, method : "GET", headers : { "Host" : urlParsed.hostname, "Proxy-Authorization" : "Basic " + base64 }};http.request(options, (res) => { console.log("got response: " + res.statusCode); // 输出返回内容(使用了gzip压缩) if (res.headers['content-encoding'] && res.headers['content-encoding'].indexOf('gzip') != -1) { let zlib = require('zlib'); let unzip = zlib.createGunzip(); res.pipe(unzip).pipe(process.stdout); } else { // 输出返回内容(未使用gzip压缩) res.pipe(process.stdout); } }) .on("error", (err) => { console.log(err); }) .end();
GO语言如何接入代码demo
发布时间 2023-09-25
// 请求代理服务器// http和https网页均适用package mainimport ( "compress/gzip" "fmt" "io" "io/ioutil" "net/http" "net/url" "os")func main() { // 用户名密码认证(私密代理/独享代理) username := "username" password := "password" // 代理服务器 proxy_raw := "proxyIp:proxyPort" proxy_str := fmt.Sprintf("http://%s:%s@%s", username, password, proxy_raw) proxy, err := url.Parse(proxy_str) // 目标网页 page_url := "http://baidu.com" // 请求目标网页 client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}} req, _ := http.NewRequest("GET", page_url, nil) req.Header.Add("Accept-Encoding", "gzip") //使用gzip压缩传输数据让访问更快 res, err := client.Do(req) if err != nil { // 请求发生异常 fmt.Println(err.Error()) } else { defer res.Body.Close() //保证最后关闭Body fmt.Println("status code:", res.StatusCode) // 获取状态码 // 有gzip压缩时,需要解压缩读取返回内容 if res.Header.Get("Content-Encoding") == "gzip" { reader, _ := gzip.NewReader(res.Body) // gzip解压缩 defer reader.Close() io.Copy(os.Stdout, reader) os.Exit(0) // 正常退出 } // 无gzip压缩, 读取返回内容 body, _ := ioutil.ReadAll(res.Body) fmt.Println(string(body)) }}
CSharp如何接入代码demo
发布时间 2023-09-25
using System;using System.Text;using System.Net;using System.IO;using System.IO.Compression;namespace csharp_http{ class Program { static void Main(string[] args) { // 要访问的目标网页 string page_url = "http://baidu.com"; // 构造请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page_url); request.Method = "GET"; request.Headers.Add("Accept-Encoding", "Gzip"); // 使用gzip压缩传输数据让访问更快 // 代理服务器 string proxy_ip = "proxyIp"; int proxy_port = proxyPort; // 用户名密码认证(私密代理/独享代理) string username = "username"; string password = "password"; // 设置代理 (开放代理或私密/独享代理&已添加白名单) // request.Proxy = new WebProxy(proxy_ip, proxy_port); // 设置代理 (私密/独享代理&未添加白名单) WebProxy proxy = new WebProxy(); proxy.Address = new Uri(String.Format("http://{0}:{1}", proxy_ip, proxy_port)); proxy.Credentials = new NetworkCredential(username, password); request.Proxy = proxy; // 请求目标网页 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode); // 获取状态码 // 解压缩读取返回内容 using (StreamReader reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))) { Console.WriteLine(reader.ReadToEnd()); } } }}