android - Complete guide to pass credentials to a server via HTTP auth and getting response back -


so know how http , post connections. http://exampledepot.com/egs/java.net/pkg.html , want pass credentials(uname,passwd) web server access url or response. , can't pass post parameters. have @ simple code all.

 try     {         // creatin connection         url url = new url("http://yoururl");         urlconnection conn = url.openconnection();      //sendin base64 encoded credentials thru d header     conn.setrequestproperty(         "authorization",         "basic "+ basicauth.encode(username, password));      // 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 credentials using simple external class named "basicauth.java" u can add in 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 name/password pair appropriate      * use in http header basic authentication.      *    name     user's name      *    passwd   user's password      *    returns  string   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 3-byte stride.          * each 'chunk' of 3-bytes, create 24-bit          * value, extract 4 6-bit indices.          * use these indices extract base-64          * encoding 6-bit 'character'          */         (int = 0; < input.length; += 3) {             int left = input.length - i;              // have @ least 3 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 2 bytes. pad 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 1 byte. pad 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);     }  } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -