隧道代理接入文档
蜻蜓代理 · 2年前 · 28123字
产品说明
隧道代理提供统一的入口地址,用户成功接入后,每一个请求都是一个随机 IP
隧道代理服务器地址
代理服务器地址为:dyn.horocn.com
代理端口为:50000
示例代码
import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class ProxyDemo {
// 代理隧道验证信息
private final static String ProxyUser = "隧道代理订单号";
private final static String ProxyPass = "密码(用户中心-我的订单页面可查)";
// 代理服务器
private final static String ProxyHost = "dyn.horocn.com";
private final static Integer ProxyPort = 50000;
private static void getUrlProxyContent(String url) {
Authenticator.setDefault(new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(ProxyUser, ProxyPass.toCharArray());
}
});
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ProxyHost, ProxyPort));
try {
Document doc = Jsoup.connect(url).timeout(3000).proxy(proxy).get();
if (doc != null) {
System.out.println(doc.body().html());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// 要访问的目标页面
String targetUrl = "https://www.baidu.com";
getUrlProxyContent(targetUrl);
}
}
from urllib import request
# 要访问的目标页面
targetUrl = "https://www.baidu.com"
# 代理服务器
proxyHost = "dyn.horocn.com"
proxyPort = "50000"
# 代理隧道验证信息
proxyUser = "隧道代理订单号"
proxyPass = "密码(用户中心-我的订单页面可查)"
proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
"host": proxyHost,
"port": proxyPort,
"user": proxyUser,
"pass": proxyPass,
}
proxy_handler = request.ProxyHandler({
"http": proxyMeta,
"https": proxyMeta,
})
opener = request.build_opener(proxy_handler)
request.install_opener(opener)
resp = request.urlopen(targetUrl).read()
print(resp)
import requests
target_url = "https://www.baidu.com"
proxy_host = 'dyn.horocn.com'
proxy_port = 50000
proxy_username = '隧道代理订单号'
proxy_pwd = "密码(用户中心-我的订单页面可查)"
proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
"host": proxy_host,
"port": proxy_port,
"user": proxy_username,
"pass": proxy_pwd,
}
proxies = {
'http': proxyMeta,
'https': proxyMeta,
}
try:
resp = requests.get(url=target_url, proxies=proxies)
print(resp.text)
except Exception as e:
print e
<?php
// 要访问的目标页面
$targetUrl = "https://www.baidu.com/";
// 代理服务器
$proxyServer = "http://dyn.horocn.com:50000";
// 隧道身份信息
// 这里的用户名,等于订单号
$proxyUser = "隧道代理订单号";
$proxyPass = "密码(用户中心-我的订单页面可查)";
$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, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
// 设置隧道验证信息
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxyUser}:{$proxyPass}");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
// 需要安装 request 库
// 使用命令:npm install request
// 仓库地址:https://github.com/request/request
const request = require('request');
var user = "隧道代理订单号"
var password = "密码(用户中心-我的订单页面可查)"
var host = "dyn.horocn.com"
var port = 50000
var proxyUrl = "http://" + user + ":" + password + "@" + host + ":" + port;
var proxiedRequest = request.defaults({'proxy': proxyUrl});
proxiedRequest.get("https://www.baidu.com", function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
})
// 要访问的目标页面
string targetUrl = "https://www.baidu.com";
// 代理服务器
string proxyHost = "http://dyn.horocn.com";
string proxyPort = "50000";
// 代理隧道验证信息
string proxyUser = "隧道代理订单号";
string proxyPass = "密码(用户中心-我的订单页面可查)";
// 设置代理服务器
var proxy = new WebProxy();
proxy.Address = new Uri(string.Format("{0}:{1}", proxyHost, proxyPort));
proxy.Credentials = new NetworkCredential(proxyUser, proxyPass);
ServicePointManager.Expect100Continue = false;
var request = WebRequest.Create(targetUrl) as HttpWebRequest;
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.Method = "GET";
request.Proxy = proxy;
using (var response = request.GetResponse() as HttpWebResponse)
using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string htmlStr = sr.ReadToEnd();
}
import scrapy
import os
# 中间件使用代理示例:https://github.com/scrapy/scrapy/blob/master/tests/test_downloadermiddleware_httpproxy.py
# 官方 HTTP 代理中间件:https://docs.scrapy.org/en/latest/topics/downloader-middleware.html?highlight=proxy#module-scrapy.downloadermiddlewares.httpproxy
class MyIPSpider(scrapy.Spider):
name = 'my_ip'
start_urls = [
'https://www.baidu.com?v=1',
'https://www.baidu.com?v=2',
]
def start_requests(self):
proxyUser = "隧道代理订单号"
proxyPass = "密码(用户中心-隧道代理订单页面可查)"
proxyHost = "dyn.horocn.com"
proxyPort = "50000"
proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
"host": proxyHost,
"port": proxyPort,
"user": proxyUser,
"pass": proxyPass,
}
for url in self.start_urls:
req = scrapy.Request(url, meta={'proxy': proxyMeta})
yield req
def parse(self, response):
yield {
'body': response.text,
}
curl --proxy-header "Proxy-Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=" --proxy http://dyn.horocn.com:50000 https://www.baidu.com
package main
import (
"github.com/parnurzeal/gorequest"
"log"
"fmt"
)
func main() {
// TODO: 请替换下面的身份信息
// 这里的用户名是隧道代理的订单号
username := "隧道代理订单号"
// 密码请到用户中心-我的订单页面查询
password := "密码(用户中心-我的订单页面可查)"
request := gorequest.New().
Proxy(fmt.Sprintf("http://%s:%s@dyn.horocn.com:50000", username, password))
resp, body, errs := request.
Get("https://www.baidu.com").
End()
if errs != nil {
log.Printf("出现异常:%s\n", errs)
return
}
log.Printf("状态码:%d\n", resp.StatusCode)
log.Printf("返回结果:%s\n", body)
}
其他示例代码
- Java
- Python3
- Node.js
注意
- Java8以上版本,请在运行的时候,加上
-Djdk.http.auth.tunneling.disabledSchemes=""
。具体原因请参考:Unable to tunnel through proxy since Java 8 Update 111 - 使用 Python requests 第三方 HTTP 网络库的过程中,如果出现 407 身份验证错误,请使用 requests 2.27.1 以上版本。官方做了相关修复。参考:https://github.com/psf/requests/pull/6028
常见错误码
错误码 | 说明 |
---|---|
407 | 需要身份验证 |
429 | 超过并发限制,请降低频率或者使用调整隧道代理套餐 |
500 | 内部服务器错误,一般是因为超时或者连接被拒绝造成的。请做好异常处理。 |