美文网首页python机器学习爬虫小石头HTTP
Kotlin使用HTTP代理和SOCKS5代理的代码示例

Kotlin使用HTTP代理和SOCKS5代理的代码示例

作者: 小石头IP客服 | 来源:发表于2024-03-28 10:47 被阅读0次

    本文档为获取到代理IP后使用代理的代码样例,供开发者参考。

    代码样例直接运行无法得到正确的结果,因为代码中的代理地址/端口和应用账号密码等信息都是虚构的,您替换成自己真实的就可以正常运行了。

    如何获取代理地址/端口和设置代理授权?

    请参考:接入指南 - 快速入门

    使用前提:

    1、 已经获取代理地址, 去获取 www.xiaoshitouip.com

    2、 已经设置代理授权(用户名/密码或者IP白名单)。

    
    package com.example
    
    import okhttp3.*
    import org.apache.http.HttpHost
    import org.apache.http.auth.AuthScope
    import org.apache.http.auth.UsernamePasswordCredentials
    import org.apache.http.client.CredentialsProvider
    import org.apache.http.client.methods.HttpGet
    import org.apache.http.impl.client.BasicCredentialsProvider
    import org.apache.http.impl.client.CloseableHttpClient
    import org.apache.http.impl.client.HttpClients
    import org.apache.http.util.EntityUtils
    import java.io.IOException
    import java.net.InetSocketAddress
    import java.net.Proxy
    import java.nio.charset.StandardCharsets
    
    fun main(args: Array<String>) {
        // 从 https://www.xiaoshitouip.com/ 领取后,替换自己的ip和端口
        val ip = "代理ip" // 代理主机地址
        val port = 代理端口 // 代理主机端口
    
        // 替换为自己的用户名
        // 到管理后台:https://admin.xiaoshitouip.com/proxy/proxy-auth 授权管理页面添加账号密码或者ip白名单
        // 设置了ip白名单就不需要设置账号密码了
        // 使用OKHttp库
        OKHttpProxy.build(ip, port).test()
    //   OKHttpProxy.build(ip, port, "代理账号", "代理密码").test(); // 代理认证
    
        // 使用HttpClient库
    //   HttpClientProxy.build(ip, port).test();
    //   HttpClientProxy.build(ip, port, "代理账号", "代理密码").test(); // 代理认证
    }
    
    /**
     * OKHttp库使用代理
     */
    internal class OKHttpProxy {
        var proxyHost: String? = null
        var proxyPort = 0
        var proxyAccount: String? = null
        var proxyPwd: String? = null
    
        @Throws(IOException::class)
        fun test() {
            val targetUrl = "http://myip.ipip.net"
            var client: OkHttpClient? = null
            client = if (proxyAccount == null || proxyPwd == null) {
                getHttpClient(proxyHost, proxyPort)
            } else {
                // 账号密码验证
                getHttpClient(proxyHost, proxyPort, proxyAccount!!, proxyPwd!!)
            }
            val request: Request = Request.Builder()
                .url(targetUrl)
                .build()
            val response: Response = client.newCall(request).execute()
            println(response.body?.string())
        }
    
        companion object {
            /**
             * @param host 代理主机地址
             * @param port 代理主机端口
             */
            fun build(host: String?, port: Int): OKHttpProxy {
                val proxy = OKHttpProxy()
                proxy.proxyHost = host
                proxy.proxyPort = port
                return proxy
            }
    
            /**
             * @param host 代理主机地址
             * @param port 代理主机端口
             * @param acc 代理认证账号
             * @param pwd 代理认证口令
             */
            fun build(host: String?, port: Int, acc: String?, pwd: String?): OKHttpProxy {
                val proxy = OKHttpProxy()
                proxy.proxyHost = host
                proxy.proxyPort = port
                proxy.proxyAccount = acc
                proxy.proxyPwd = pwd
                return proxy
            }
    
            /**
             * 代理不需要账号密码认证的httpClient
             */
            private fun getHttpClient(proxyHost: String?, proxyPort: Int): OkHttpClient {
                val proxy = Proxy(Proxy.Type.HTTP,
     InetSocketAddress(proxyHost, proxyPort))
                return OkHttpClient.Builder()
                    .proxy(proxy)
                    .build()
            }
    
            /**
             * 代理需要账号密码认证的httpClient
             */
            private fun getHttpClient(proxyHost: String?, 
    proxyPort: Int, acc: String, pwd: String): OkHttpClient {
                val proxy = Proxy(Proxy.Type.HTTP,
     InetSocketAddress(proxyHost, proxyPort))
                // 账号密码验证
                val authenticator: Authenticator = Authenticator { _, response ->
                    val credential: String = Credentials.basic(acc, pwd)
                    response.request.newBuilder()
    .header("Proxy-Authorization", credential).build()
                }
                return OkHttpClient.Builder()
                    .proxy(proxy)
                    .proxyAuthenticator(authenticator)
                    .build()
            }
        }
    }
    
    /**
     * HttpClient库使用代理
     */
    internal class HttpClientProxy {
        var proxyHost: String? = null
        var proxyPort = 0
        var proxyAccount: String? = null
        var proxyPwd: String? = null
    
        @Throws(IOException::class)
        fun test() {
            val targetUrl = "http://myip.ipip.net"
            var client: CloseableHttpClient? = null
            client = if (proxyAccount == null || proxyPwd == null) {
                getHttpClient(proxyHost, proxyPort)
            } else {
                // 账号密码验证
                getHttpClient(proxyHost, proxyPort, proxyAccount!!, proxyPwd!!)
            }
            val httpGet = HttpGet(targetUrl)
            val response = client.execute(httpGet)
            val resultStr: String = EntityUtils.toString(response.entity, 
    StandardCharsets.UTF_8)
            println(resultStr)
        }
    
        companion object {
            /**
             * @param host 代理主机地址
             * @param port 代理主机端口
             */
            fun build(host: String?, port: Int): HttpClientProxy {
                val proxy = HttpClientProxy()
                proxy.proxyHost = host
                proxy.proxyPort = port
                return proxy
            }
    
            /**
             * @param host 代理主机地址
             * @param port 代理主机端口
             * @param acc 代理认证账号
             * @param pwd 代理认证口令
             */
            fun build(host: String?, port: Int, acc: String?, pwd: String?): 
    HttpClientProxy {
                val proxy = HttpClientProxy()
                proxy.proxyHost = host
                proxy.proxyPort = port
                proxy.proxyAccount = acc
                proxy.proxyPwd = pwd
                return proxy
            }
    
            /**
             * 代理不需要账号密码认证的httpClient
             */
            private fun getHttpClient(proxyHost: String?, proxyPort: Int): 
    CloseableHttpClient {
                val proxy = HttpHost(proxyHost, proxyPort, "HTTP")
                return HttpClients.custom()
                    .setProxy(proxy)
                    .build()
            }
    
            /**
             * 代理需要账号密码认证的httpClient
             */
            private fun getHttpClient(proxyHost: String?,
     proxyPort: Int, acc: String, pwd: String): CloseableHttpClient {
                val proxy = HttpHost(proxyHost, proxyPort, "HTTP")
                val provider: CredentialsProvider = BasicCredentialsProvider()
                provider.setCredentials(AuthScope(proxy), 
    UsernamePasswordCredentials(acc, pwd))
                return HttpClients.custom()
                    .setProxy(proxy)
                    .setDefaultCredentialsProvider(provider)
                    .build()
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:Kotlin使用HTTP代理和SOCKS5代理的代码示例

        本文链接:https://www.haomeiwen.com/subject/ofyutjtx.html