<?php
// 要访问的目标页面
$targetUrl = "http://baidu.com";
// 代理服务器
$proxyServer = "http://proxyIp:proxyPort";
// 隧道身份信息
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 设置代理服务器
//curl_setopt($ch, CURLOPT_PROXYTYPE, 0); //http
curl_setopt($ch, CURLOPT_PROXYTYPE, 5); //sock5
curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
// 设置隧道验证信息
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
#!/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)
import okhttp3.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
public class TestProxyOKHttpClient {
public static void main(String args[]) throws IOException {
// 目标网站
String targetUrl = "http://baidu.com";
// 用户名密码认证(私密代理/独享代理)
final String username = "username";
final String password = "password";
String ip = "proxyIp"; // 代理服务器IP
int port = proxyPort;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
Authenticator authenticator = new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
OkHttpClient client = new OkHttpClient.Builder()
.proxy(proxy)
.proxyAuthenticator(authenticator)
.build();
Request request = new Request.Builder()
.url(targetUrl)
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
}
const http = require("http"); // 引入内置http模块
const url = require("url");
// 要访问的目标页面
const targetUrl = "http://baidu.com";
const urlParsed = url.parse(targetUrl);
// 代理ip
const proxyIp = "proxyIp"; // 代理服务器ip
const 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()
;
// 请求代理服务器
// http和https网页均适用
package main
import (
"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))
}
}
# -*- coding: utf-8 -*-
require 'net/http' # 引入内置net/http模块
require 'zlib'
require 'stringio'
# 代理服务器ip 和 端口
proxy_ip = 'proxyIp'
proxy_port = proxyPort
# 要访问的目标网页, 以百度页面为例
page_url = "http://baidu.com"
uri = URI(page_url)
# 新建代理实例
proxy = Net::HTTP::Proxy(proxy_ip, proxy_port)
# 创建新的请求对象
req = Net::HTTP::Get.new(uri)
# 设置User-Agent
req['User-Agent'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
req['Accept-Encoding'] = 'gzip' # 使用gzip压缩传输数据让访问更快
# 使用代理发起请求, 若访问的是http网页, 请将use_ssl设为false
res = proxy.start(uri.hostname, uri.port, :use_ssl => true) do |http|
http.request(req)
end
# 输出状态码
puts "status code: #{res.code}"
# 输出响应体
if res.code.to_i != 200 then
puts "page content: #{res.body}"
else
gz = Zlib::GzipReader.new(StringIO.new(res.body.to_s))
puts "page content: #{gz.read}"
end
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());
}
}
}
}