Android's Okhttp package handles user-authenticated code instance sharing _android

Source: Internet
Author: User

Okhttp provides support for user authentication. When the status code for the HTTP response is 401, okhttp gets the new request object from the Set authenticator object and attempts to make the request again. The Authenticate method in the authenticator interface is used to provide a request object for authentication, and the Authenticateproxy method is used to provide a request object that authenticates the proxy server.
Examples of user authentication:

Okhttpclient client = new Okhttpclient ();
Client.setauthenticator (New Authenticator () {public
Request authenticate (proxy proxy, Response Response) throws IOException {
  String credential = credentials.basic ("User", "password");
  Return Response.request (). Newbuilder ().
      Header ("Authorization", credential)
      . Build ();

Public Request authenticateproxy (proxy proxy, Response Response) 
throws IOException {return
  null;
}
} );

Advanced
when you need to implement a Basic challenge, use Credentials.basic (username, password) to encode the request headers.

Private final Okhttpclient client = new Okhttpclient (); public void Run () throws Exception {Client.setauthenticator (New authenticator () {@Override public Request Authenticat
   E (proxy proxy, Response Response) {System.out.println ("Authenticating for Response:" + Response);
   System.out.println ("Challenges:" + response.challenges ());
   String credential = Credentials.basic ("Jesse", "Password1");
  Return Response.request (). Newbuilder (). Header ("Authorization", credential). Build (); @Override public Request authenticateproxy (proxy proxy, Response Response) {return null;//null indicates no ATT
  Empt to authenticate.

 }
 });

 Request Request = new Request.builder (). URL ("Http://publicobject.com/secrets/hellosecret.txt"). Build ();
 Response Response = client.newcall (Request). Execute ();

 if (!response.issuccessful ()) throw new IOException ("Unexpected Code" + response);
System.out.println (Response.body (). String ()); }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.