/* * ==================================================================== * Licensed to the Apache software Foundation ( ASF) under one * or more contributor license agreements. See the NOTICE file * Distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * under the Apache License, Version 2.0 (The * "License"); You are not a use of this file except in compliance * with the License. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicable Law or agreed into writing, * software distributed under the License is distributed on A * "As is" BASIS, without Warra Nties or CONDITIONS of any * KIND, either express OR implied. See the License for the * Specific language governing permissions and limitations * under the License. * ==================================================================== * * This software consists of voluntary Contributions made by many * INdividuals on behalf of the Apache software Foundation. For more * information on the Apache software Foundation, please see * <HTTP://WWW.APACHE.ORG/>. * */package org.apache.http.examples.client;import Org.apache.http.httphost;import org.apache.http.auth.AuthScope; Import Org.apache.http.auth.usernamepasswordcredentials;import Org.apache.http.client.authcache;import Org.apache.http.client.credentialsprovider;import Org.apache.http.client.methods.closeablehttpresponse;import Org.apache.http.client.methods.httpget;import Org.apache.http.client.protocol.httpclientcontext;import Org.apache.http.impl.auth.basicscheme;import Org.apache.http.impl.client.basicauthcache;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;/** * An example of HttpClient Can is customized to authenticate * preemptively using BASIC scheme. * <b> * GeneraLly, preemptive authentication can be considered less * secure than a response to an authentication challenge * and Theref Ore discouraged. */public class Clientpreemptivebasicauthentication {public static void main (string[] args) throws Exception {H Ttphost target = new Httphost ("httpbin.org", "http"); Credentialsprovider Credsprovider = new Basiccredentialsprovider (); Credsprovider.setcredentials (New Authscope (Target.gethostname (), Target.getport ()), new use Rnamepasswordcredentials ("User", "passwd")); Closeablehttpclient httpclient = Httpclients.custom (). Setdefaultcredentialsprovider (Credsprovider). Build () ; try {//Create Authcache instance Authcache authcache = new Basicauthcache (); Generate BASIC Scheme object and add it to the local//auth cache basicscheme BasicAuth = new Ba Sicscheme (); Authcache.put (target, BasicAuth); Add Authcache to the execution context Httpclientcontext Localcontext = Httpclientcontext.create (); Localcontext.setauthcache (Authcache); HttpGet httpget = new HttpGet ("http://httpbin.org/hidden-basic-auth/user/passwd"); SYSTEM.OUT.PRINTLN ("Executing request" + httpget.getrequestline () + "to target" + target); for (int i = 0; i < 3; i++) {Closeablehttpresponse response = Httpclient.execute (target, HttpGet, local Context); try {System.out.println ("----------------------------------------"); System.out.println (Response.getstatusline ()); System.out.println (Entityutils.tostring (Response.getentity ())); } finally {response.close (); }}} finally {Httpclient.close (); } }}
JAVA Http Basic auth