JAVA Http Basic Auth

Source: Internet
Author: User

/* * ==================================================================== * 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 * &LT;HTTP://WWW.APACHE.ORG/&GT;. * */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

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.