API文档中心

为开发者提供最易用的API接口, 批量部署不再难!


HTTP Proxy Demo 代码

1、Python

    #! -*- encoding:utf-8 -*-

    import requests

    # 要访问的目标页面
    targetUrl = "http://api.wuxianip.com/ip"

    # 代理服务器
    proxyHost = "api.wuxianip.com"
    proxyPort = "39010"

    # 代理隧道验证信息
    proxyUser = "username"
    proxyPass = "password"

    proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
        "host" : proxyHost,
        "port" : proxyPort,
        "user" : proxyUser,
        "pass" : proxyPass,
    }

    proxies = {
        "http"  : proxyMeta,
        "https" : proxyMeta,
    }

    resp = requests.get(targetUrl, proxies=proxies)

    print resp.status_code
    print resp.text

2、C#

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.wuxianip.com/ip");

    WebProxy myProxy = new WebProxy();

    Uri newUri = new Uri("http://api.wuxianip.com:39010");

    myProxy.Address = newUri;

    myProxy.Credentials = new NetworkCredential("username", "password");


    request.Proxy = myProxy;

3、PHP

    // 要访问的目标页面
    $targetUrl = "http://api.wuxianip.com/ip";
    //$targetUrl = "http://api.wuxianip.com/switch-ip";
    //$targetUrl = "http://api.wuxianip.com/current-ip";
    // 代理服务器
    define("PROXY_SERVER", "api.wuxianip.com:39010");
    // 隧道身份信息
    define("PROXY_USER", "username");
    define("PROXY_PASS", "password");
    $proxyAuth = base64_encode(PROXY_USER . ":" . PROXY_PASS);
    $headers = implode("\r\n", [
        "Proxy-Authorization: Basic ",
        "Proxy-Switch-Ip: yes",
    ]);
    $options = [
        "http" => [
            "proxy"  => $proxyServer,
            "header" => $headers,
            "method" => "GET",
        ],
    ];
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    var_dump($result);

4、JAVA

    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.net.Authenticator;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.PasswordAuthentication;
    import java.net.Proxy;
    import java.net.URL;

    class ProxyAuthenticator extends Authenticator {
        private String user, password;

        public ProxyAuthenticator(String user, String password) {
            this.user     = user;
            this.password = password;
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password.toCharArray());
        }
    }

    /**
     * 注意:下面代码仅仅实现HTTP请求链接,每一次请求都是无状态保留的,仅仅是这次请求是更换IP的,如果下次请求的IP地址会改变
     * 如果是多线程访问的话,只要将下面的代码嵌入到你自己的业务逻辑里面,那么每次都会用新的IP进行访问,如果担心IP有重复,
     * 自己可以维护IP的使用情况,并做校验。
     */
    public class ProxyDemo {
        public static void main(String args[]) throws Exception {
            // 要访问的目标页面
            String targetUrl = "http://api.wuxianip.com/ip";
            //String targetUrl = "http://api.wuxianip.com/switch-ip";
            //String targetUrl = "http://api.wuxianip.com/current-ip";

            // 代理服务器
            String proxyServer = "api.wuxianip.com";
            int proxyPort      = 39010;

            // 代理隧道验证信息
            String proxyUser  = "username";
            String proxyPass  = "password";

            try {
                URL url = new URL(targetUrl);

                Authenticator.setDefault(new ProxyAuthenticator(proxyUser, proxyPass));

                // 创建代理服务器地址对象
                InetSocketAddress addr = new InetSocketAddress(proxyServer, proxyPort);
                // 创建HTTP类型代理对象
                Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);

                // 设置通过代理访问目标页面
                HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
                // 设置IP切换头
                connection.setRequestProperty("Proxy-Switch-Ip","yes");

                // 解析返回数据
                byte[] response = readStream(connection.getInputStream());

                System.out.println(new String(response));
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
        }

        /**
         * 将输入流转换成字符串
         *
         * @param inStream
         * @return
         * @throws Exception
         */
        public static byte[] readStream(InputStream inStream) throws Exception {
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;

            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            outSteam.close();
            inStream.close();

            return outSteam.toByteArray();
        }
    }

5、golang

    package main

    import (
        "net/url"
        "net/http"
        "bytes"
        "fmt"
        "io/ioutil"
    )

    const ProxyServer = "api.wuxianip.com:39010"

    type ProxyAuth struct {
        License string
        SecretKey string
    }

    func (p ProxyAuth) ProxyClient() http.Client {
        proxyURL, _ := url.Parse("http://" + p.License + ":" + p.SecretKey + "@" + ProxyServer)
        return http.Client{Transport: &http.Transport{Proxy:http.ProxyURL(proxyURL)}}
    }

    func main()  {
        targetURI := "http://ip.hahaod.cn/ip"
        //targetURI := "http://ip.hahaod.cn/switch-ip"
        //targetURI := "http://ip.hahaod.cn/current-ip"

        // 初始化 proxy http client
        client := ProxyAuth{License: "username", SecretKey: "password"}.ProxyClient()

        request, _ := http.NewRequest("GET", targetURI, bytes.NewBuffer([] byte(``)))

        // 切换IP (只支持 HTTP)
        request.Header.Set("Proxy-Switch-Ip", "yes")

        response, err := client.Do(request)

        if err != nil {
            panic("failed to connect: " + err.Error())
        } else {
            bodyByte, err := ioutil.ReadAll(response.Body)
            if err != nil {
                fmt.Println("读取 Body 时出错", err)
                return
            }
            response.Body.Close()

            body := string(bodyByte)

            fmt.Println("Response Status:", response.Status)
            fmt.Println("Response Header:", response.Header)
            fmt.Println("Response Body:\n", body)
        }
    }

如需帮助请直接联系无限IP客服或者邮件:sales@wuxianip.com

Copyright © 2015-2017 云汇科技无限IP(wuxianip.com) 闽ICP备15024699号   闽公网安备 35020602000514号

客服 小虫 客服 Yvon 技术支持