Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6253919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:06:32+00:00 2026-05-24T14:06:32+00:00

What is the best way to use preemptive basic http authentication using HttpUrlConnection. (Assume

  • 0

What is the best way to use preemptive basic http authentication using HttpUrlConnection. (Assume for now I can’t use HttpClient).

EDIT for clarification: I’m setting the un/pw correctly in the request header using Base64 encoding. Are there any additional flags or properties that need to be set, or is the fact that I’m setting the basic auth headers for the request all that is needed for preemptive basic auth?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:06:32+00:00Added an answer on May 24, 2026 at 2:06 pm

    If you are using Java 8 or later, java.util.Base64 is usable:

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    String encoded = Base64.getEncoder().encodeToString((username+":"+password).getBytes(StandardCharsets.UTF_8));  //Java 8
    connection.setRequestProperty("Authorization", "Basic "+encoded);
    

    Then use the connection as normal.

    If you’re using Java 7 or lower, you’ll need a method to encode a String to Base64, such as:

    byte[] message = (username+":"+password).getBytes("UTF-8");
    String encoded = javax.xml.bind.DatatypeConverter.printBase64Binary(message);
    

    Yes, that’s all you have to do in order to use Basic Auth. The code above to set the Request Property should be done immediately after opening the connection and before getting the Input or Output streams.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what will be best way to use using google.loader.ClientLocation to get client country &
What is the best way to use Apache Click ( http://click.apache.org/ ) with GWT?
What is the best way to use live id authentication with azure based asp.net
Can anyone recommend the best way to use version control system in Visual Studio
What is the best way to use SQL Server 2008 as a development database,
What is the best way to use ResolveUrl() in a Shared/static function in Asp.Net?
What's the best way to use QT4's QItemDelegate to show thumbnails for images in
What is the best way to use multiple EVAL fields in a GridView ItemTemplate?
What is the best way to use ListView and a set of GroupBoxes as
What's the best way to use Bazaar (bzr) as the version control system in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.