繁体   English   中英

Kotlin Ktor客户端403禁止cloudflare

[英]Kotlin Ktor client 403 forbidden cloudflare

因此,我试图请求一方使用受 cloudflare 保护的代理。 问题是我收到 403 forbidden cloduflare 错误,但只有当我使用没有它的代理时才有效。 但是代理不是我用python(请求模块)尝试过的问题,并且在我的浏览器中我没有被阻止。 我的代码

suspend fun scrape() {
    val client = HttpClient {
        followRedirects = true

        install(ContentNegotiation) {
            json(Json {
                ignoreUnknownKeys = true
            })
        }
        engine {
            proxy =
                ProxyBuilder.http("http://ProxyIP:proxyPort")

        }
        defaultRequest {
            val credentials = Base64.getEncoder().encodeToString("ProxyUser:ProxyPassword".toByteArray())
            header(HttpHeaders.ProxyAuthorization, "Basic $credentials")

        }



    }
    val response = client.get("http://example.com")
    val body = response.bodyAsText()
    println(body)
    println(response.status.hashCode())

修复它

suspend fun scrape() {
    val client = HttpClient(Apache) {

        install(ContentNegotiation) {
            json(Json {
                ignoreUnknownKeys = true
            })
        }
        engine {
            followRedirects = false
            customizeClient  {
                setProxy(HttpHost("hostname", port))

                val credentialsProvider = BasicCredentialsProvider()
                credentialsProvider .setCredentials(
                    AuthScope("hostname", port),
                    UsernamePasswordCredentials("username", "password")
                )
                setDefaultCredentialsProvider(credentialsProvider )


            }
        }

    }

    val response =
        client.get("http://example.com") {
        }
    val body = response.bodyAsText()
    println(body)
    println(response.status.hashCode())
}


使用CIO引擎通过代理服务器发出请求时出现问题。 我创建了一个问题来解决这个问题。 作为解决方法,请使用OkHttp引擎而不是CIO

以下是如何使用具有基本身份验证的代理:

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.request.*
import kotlinx.coroutines.runBlocking
import okhttp3.Authenticator
import okhttp3.Credentials
import okhttp3.OkHttpClient
import java.net.Proxy


fun main(): Unit = runBlocking {
    val proxyAuthenticator = Authenticator { _, response ->
        response.request.newBuilder()
            .header("Proxy-Authorization", Credentials.basic("<username>", "<password>"))
            .build()
    }

    val client = HttpClient(OkHttp) {
        engine {
            preconfigured = OkHttpClient.Builder()
                .proxy(Proxy(Proxy.Type.HTTP, java.net.InetSocketAddress("127.0.0.1", 3128)))
                .proxyAuthenticator(proxyAuthenticator)
                .build()
        }
    }

    val response = client.get("http://eu.kith.com/products.json")
    println(response.status)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM