Android OkHttp with Basic Authentication

后端 未结 12 1501
感动是毒
感动是毒 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条回答
  •  有刺的猬
    2020-12-07 13:37

    All answers are good but no one said, that for some requests content-type is required, you should add a content-type to your request like this:

    Request request = new Request.Builder()
            .url(url)
            .addHeader("content-type", "application/json") 
            .post(body)
            .build();
    

    If you don't add it, you will get Unauthorized message and you will waste a lot of time to fix it.

提交回复
热议问题