Tutorial :Java HTTP AUTH?


I'm trying to connect a desktop application I am writing with the del.icio.us api @ Delicious API and simply provide them with my username and password and to request an url to post a bookmark to my profile.

The problem I have is that I don't understand how to send my login credentials when I open a connection.

How would I go about doing this?


From the site you referenced:

All /v1 api's require https requests and HTTP-Auth.

HTTP-Auth is header used in basic authentication.
In Java, you can simply put your credentials in the URL:


You can verify that it was set correctly using the URL.getUserInfo() method.


HttpUrlConnection allows you to perform basic authentication. The more powerful HttpClient libraru offers you more solutions (basic, digest, and NTLM - which I don't think you need).


If you just need HTTP Basic Authentication, you can form your URL like this:



Using the example provided, this should work:



HttpURLConnection does allow you to do basic auth, but if you want more complex authentication to be able to work, give Apache HTTPClient a shot. It can seamlessly-handle http authentication schemes like Basic and Digest without you even noticing.

HTTPClient Authentication Guide


This Answer is ok:

public String reloadTomcatWebApplication(String user, String pwd, String urlWithParameters, boolean returnResponse){              URL url = null;      try {            url = new URL(urlWithParameters);      } catch (MalformedURLException e) {          System.out.println("MalformedUrlException: " + e.getMessage());          e.printStackTrace();          return "-1";      }        URLConnection uc = null;      try {          uc = url.openConnection();      } catch (IOException e) {          System.out.println("IOException: " + e.getMessage());          e.printStackTrace();           return "-12";      }          String userpass = user + ":" + pwd;      String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());        uc.setRequestProperty ("Authorization", basicAuth);        InputStream is = null;      try {          is = uc.getInputStream();      } catch (IOException e) {          System.out.println("IOException: " + e.getMessage());          e.printStackTrace();          return "-13";      }      if(returnResponse){      BufferedReader buffReader = new BufferedReader(new InputStreamReader(is));      StringBuffer response = new StringBuffer();        String line = null;      try {          line = buffReader.readLine();      } catch (IOException e) {          e.printStackTrace();          return "-1";      }        while (line != null) {          response.append(line);          response.append('\n');          try {              line = buffReader.readLine();          } catch (IOException e) {              System.out.println(" IOException: " + e.getMessage());              e.printStackTrace();              return "-14";          }      }        try {          buffReader.close();      } catch (IOException e) {          e.printStackTrace();          return "-15";      }             System.out.println("Response: " + response.toString());      return response.toString();        }                     return "0";  }  


I tried this for the delicious api and it worked like a charm

import urllib2,base64  req=urllib2.Request("https://api.del.icio.us/v1/posts/update")  username="Replace with your Username"  password="Replace with your password"  base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')  req.add_header("Authorization", "Basic %s" % base64string)   response=urllib2.urlopen(req)  print response.read()  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »