Connect to a password protected URL from Java program

By: Mike Cooper Viewed: 153135 times  Printer Friendly Format    


It is quite common for a Java program to connect to an URL and process its data or post data to it. Some URLs are password protected and can be connected to it only with a username and password. While manually accessing such sites, the browser will popup a login screen automatically for the user to key in username and password. But if you are connecting from a Java program then how can you do this login programmatically.

This sample Java program connects to password protected URL. The three arguments for this application is the password protected url to connect to, the username and the password. Take note that the System properties to set the proxy has to be changed to suit your own network proxy settings. If your network doesn't need a proxy then just set the proxy to false.

import java.io.*;
import java.net.*;
import java.util.*;
public class postto{

private void fetchURL (String urlString,String user,String pass) {

try {
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;

Properties sysProperties = System.getProperties();

sysProperties.put("proxyHost", "proxy.cyberway.com.sg");
sysProperties.put("proxyPort", "8080");
sysProperties.put("proxySet", "true");
url = new URL (urlString);
urlConn = url.openConnection();
urlConn.setDoInput (true);

urlConn.setDoOutput (true);

urlConn.setUseCaches (false);

urlConn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");

printout = new DataOutputStream (urlConn.getOutputStream ());

String content = "USERNAME=" + URLEncoder.encode (user) + "&PASSWORD=" + URLEncoder.encode (pass);

printout.writeBytes (content);
printout.flush ();
printout.close ();

input = new DataInputStream (urlConn.getInputStream ());
FileOutputStream fos=new FileOutputStream("postto.txt");
String str;
while (null != ((str = input.readLine())))
{

if (str.length() >0)
{
fos.write(str.getBytes());
fos.write(new String("\n").getBytes());
}
}
input.close ();
}
catch(MalformedURLException mue){ System.out.println (mue);}
catch(IOException ioe){ System.out.println (ioe);}
}
public static void main (String args[]) {
postto au=new postto();
au.fetchURL(args[0],args[1],args[2]);
}
}


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Thanks for the help, however I am having some diff
View Tutorial          By: Andrew at 2008-09-30 14:51:14

2. i want to know how to connect one page to another
View Tutorial          By: mahesh at 2009-08-26 01:31:40

3. Hi Andrew,

I have a java applicatio

View Tutorial          By: kumar at 2010-10-06 10:42:55

4. plz send the coad for yahoo.com
View Tutorial          By: mohd at 2011-08-07 15:39:18

5. Thanks a lot for sharing this solution.
View Tutorial          By: Bharath at 2012-12-18 18:32:33

6. Hey thanks for your tutorial.

But i

View Tutorial          By: Matze at 2013-07-19 16:08:46

7. Hey thanks for your tutorial. But i'm getting an e
View Tutorial          By: acc at 2013-07-25 14:30:50

8. Awesome post! I read your blog fairly often and yo
View Tutorial          By: mobile marketing at 2017-04-01 01:36:13

9. Spot on with this write-up, I really feel this sit
View Tutorial          By: 403 ______ at 2017-04-01 16:30:22

10. Do you have any video of that? I'd love to find ou
View Tutorial          By: http://mymp3.co/Songs at 2017-04-06 07:04:45

11. JasonNix
View Tutorial          By: JasonNix at 2017-04-12 23:32:44

12. Undeniably consider that which you stated. Your fa
View Tutorial          By: ZPETS at 2017-05-21 09:49:26

13. My spouse and I stumbled over here by a different
View Tutorial          By: Cracking at 2017-06-16 23:17:09

14. Touche. Great arguments. Keep up the good spirit.
View Tutorial          By: allynjullyx at 2017-07-30 22:53:50

15. You have made some decent points there. I checked
View Tutorial          By: www.babescamshows.com at 2017-07-31 05:55:48

16. Drag it into the ccue then drag up a button to sho
View Tutorial          By: www.pearltrees.com at 2017-08-18 07:15:43

cannot update times viewed