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 512239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:17:58+00:00 2026-05-13T07:17:58+00:00

So we know how to do http get and post connections. http://exampledepot.com/egs/java.net/pkg.html And we

  • 0

So we know how to do http get and post connections.
http://exampledepot.com/egs/java.net/pkg.html
And we want to pass credentials(uname,passwd) to any web server to access the url or get the response.
And we can’t pass it as post parameters.
So have a look @ this very simple code which does it all.

  • 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-13T07:17:58+00:00Added an answer on May 13, 2026 at 7:17 am
     try
        {
            // Creatin the connection
            URL url = new URL("http://yoururl");
            URLConnection conn = url.openConnection();
    
        //sendin the base64 encoded credentials thru d header
        conn.setRequestProperty(
            "Authorization",
            "Basic "+ BasicAuth.encode(username, password));
    
        // Get the response
        BufferedReader rd = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    
        //readin d response till d end
        while ((line = rd.readLine()) != null) {
            // Process line...
            Log.v("", line);
        }
    
        rd.close();
    }
    catch (Exception e) { }
    

    To encode the credentials I am using a simple external class named “BasicAuth.java” which u can just add in to your project.
    BasicAuth.java

    public class BasicAuth {
     private BasicAuth() {}
    
        // conversion table
        private static byte[] cvtTable = {
            (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E',
            (byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J',
            (byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O',
            (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T',
            (byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y',
            (byte)'Z',
            (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e',
            (byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j',
            (byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o',
            (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t',
            (byte)'u', (byte)'v', (byte)'w', (byte)'x', (byte)'y',
            (byte)'z',
            (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
            (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
            (byte)'+', (byte)'/'
        };
    
        /**
         * Encode a name/password pair appropriate to
         * use in an HTTP header for Basic Authentication.
         *    name     the user's name
         *    passwd   the user's password
         *    returns  String   the base64 encoded name:password
         */
        static String encode(String name,
                             String passwd) {
            byte input[] = (name + ":" + passwd).getBytes();
            byte[] output = new byte[((input.length / 3) + 1) * 4];
            int ridx = 0;
            int chunk = 0;
    
            /**
             * Loop through input with 3-byte stride. For
             * each 'chunk' of 3-bytes, create a 24-bit
             * value, then extract four 6-bit indices.
             * Use these indices to extract the base-64
             * encoding for this 6-bit 'character'
             */
            for (int i = 0; i < input.length; i += 3) {
                int left = input.length - i;
    
                // have at least three bytes of data left
                if (left > 2) {
                    chunk = (input[i] << 16)|
                            (input[i + 1] << 8) |
                             input[i + 2];
                    output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
                    output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
                    output[ridx++] = cvtTable[(chunk&0xFC0)   >> 6];
                    output[ridx++] = cvtTable[(chunk&0x3F)];
                } else if (left == 2) {
                    // down to 2 bytes. pad with 1 '='
                    chunk = (input[i] << 16) |
                            (input[i + 1] << 8);
                    output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
                    output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
                    output[ridx++] = cvtTable[(chunk&0xFC0)   >> 6];
                    output[ridx++] = '=';
                } else {
                    // down to 1 byte. pad with 2 '='
                    chunk = input[i] << 16;
                    output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
                    output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
                    output[ridx++] = '=';
                    output[ridx++] = '=';
                }
            }
            return new String(output);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 318k
  • Answers 318k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would pick up Head First Servlets and JSP or… May 13, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer You have to mark the position of the text with… May 13, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer This is certainly doable. I cooked up an example that… May 13, 2026 at 11:51 pm

Related Questions

I want to programmatically download the contents of a web page, but that page
this is my first post on this website, but I'm all the time getting
Im checking the examples google gives on how to start using python; especifically the
I have been tasked with looking for a performance testing solution for one of
We have this software that has a webservices component. Now, the administrator of this

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.