Android OkHttp with Basic Authentication

后端 未结 12 1556
感动是毒
感动是毒 2020-12-07 12:58

I\'m using the OkHttp library for a new project and am impressed with its ease of use. I now have a need to use Basic Authentication. Unfortunately, there is a dearth of w

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 13:45

    Someone asked for a Kotlin version of the interceptor. Here is what I came up with and it works great:

            val client = OkHttpClient().newBuilder().addInterceptor { chain ->
            val originalRequest = chain.request()
    
            val builder = originalRequest.newBuilder()
                    .header("Authorization", Credentials.basic("ausername", "apassword"))
            val newRequest = builder.build()
            chain.proceed(newRequest)
        }.build()
    

提交回复
热议问题