帮助文档>代理IP>操作指南>调用方式 > 代码示例--隧道代理

代码示例--隧道代理

发布时间:2022-03-18 14:06

1.隧道示例使用说明

(1). 代码样例不能直接运行,因为代码中的隧道服务器域名tunne11.qg.net、端口号16519、用户名AuthKey、密码password都是虚构的,请替换成您自己的信息,可在隧道业务详情页中查看。查看我的隧道信息>>

(2). 隧道代理不需要使用API链接等其他方式获取代理IP,所有请求将在隧道服务器上进行切换IP并转发。

(3). 建议关闭HTTP协议的keep-alive功能,避免因连接复用导致隧道不能切换IP。

(4). 使用代码样例过程中遇到问题请联系售后客服,我们会为您提供技术支持。

2.代码示例—C#

此代码适用.NET 5.0 and .NET Core

此示例通过WebProxy进行代理设置,并通过HttpWebResquest发送请求。

下面的代码需要用到命名空间 System.Net、System.Text、System.IO,请先引入命名空间

此代码以http和https代理为例。

using System;
using System.Net;
using System.Text;
using System.IO;
namespace proxy_demo
{
class Program
{
static void Main(string[] args)
{
sendRequest("[https://myip.ipip.net](https://myip.ipip.net/)", SetProxy());
}
private static void sendRequest(String urlStr, WebProxy proxyObj)
{
try
{
// 设置Http/https请求
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(urlStr);
httpRequest.Method = "GET";
httpRequest.Credentials = CredentialCache.DefaultCredentials;
// 在发起HTTP请求前将proxyObj赋值给HttpWebRequest的Proxy属性
httpRequest.Proxy = proxyObj;
// 抓取响应返回的页面数据
HttpWebResponse res = (HttpWebResponse)httpRequest.GetResponse();
StreamReader reader = new StreamReader(res.GetResponseStream(),System.Text.Encoding.UTF8);
string content = reader.ReadToEnd();
reader.Close();
Console.WriteLine("{0}", content);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static WebProxy SetProxy()
{
WebProxy proxy = new WebProxy();
try
{
// 设置代理属性WebProxy
string server = @"tunnel2.qg.net:18519"; //隧道服务器地址
string proxyUser = @"4B2AF3A6"; //密钥
string proxyPass = @"D95133B9A167"; //密码
proxy.Address = new Uri(string.Format("http://{0}/",server));
proxy.Credentials = new System.Net.NetworkCredential(proxyUser, proxyPass); //如使用白名单方式连接,可注释此行
Console.WriteLine("隧道服务器连接成功:{0}", server);
return proxy;
}
catch (NullReferenceException e)
{
Console.WriteLine("隧道服务器连接失败,请检查隧道服务器地址、密钥账号、密码是否有误。" e.ToString());
}
return proxy;
}
}
}

 

运行结果:

  1. {"errorcode":-46628,"errormsg":"htm file not exist, retcode:-21006"}

3.代码示例—PHP

以下示例适用于php5及php7

3.1 库

下面的代码需要用到php的curl库,请预先安装并开启

3.2 代码

$url = 'https://httpbin.org/get';
//获取到的隧道地址和端口
$proxy = 'tunnel2.qg.net:18519';
//用户的key和密码,使用账密模式访问隧道代理的时候用到。如果使用IP白名单模式,请注释掉下行
$proxyauth = 'key:passwd';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//设置代理
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//使用账密模式访问代理时设置账密。如果使用IP白名单模式,请注释掉下行
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
//设置使用的代理类型,当前为socks5类型,如果不设置,默认为http/https类型
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
if ( ! $res = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
echo $res;

4.代码示例-Python

以下示例适用于python2 及 python3

4.1 库

进行http请求,我们一般使用requests库,该库支持http/https代理。 安装如下

  1. pip install requests

如果需要使用socks5代理,可以安装支持socks的版本

  1. pip install 'requests[socks]'

4.2 代码

下面以使用socks5代理为例

  1. ```python
  2. import requests
  3.  
  4. proxyHost = "tunnel2.qg.net" #获取隧道服务器地址
  5. proxyPort = "18519" #获取隧道服务器端口
  6. key = "key" #用户key
  7. passwd = "123456" #用户密码
  8.  
  9. # 账密模式
  10. proxy = 'socks5://{}:{}@{}:{}'.format(key, passwd, proxyHost, proxyPort)
  11. # 如果使用IP白名单模式,请用下行替换上一行
  12. # proxy = 'socks5://{}:{}'.format(proxyHost, proxyPort)
  13.  
  14. proxies = {
  15. "http": proxy,
  16. "https": proxy
  17. }
  18.  
  19. response = requests.get("https:///www.fwvps.com", proxies=proxies)
  20. print(response.text)
  21. ```

如果想使用http/https代理,请修改接入代理的协议,如下

  1.  
  2. ```python
  3. # 账密模式
  4. proxy = 'http://{}:{}@{}:{}'.format(key, passwd, proxyHost, proxyPort)
  5. # 如果使用IP白名单模式,请用下行替换上一行
  6. # proxy = 'http://{}:{}'.format(proxyHost, proxyPort)
  7. ```

本文导读

客户热线:4007-567-365