Connect to a password protected URL from Java program

By: Mike Cooper  

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]);
}
}



Archived Comments

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

2. You have made some decent points there. I checked on the net
for more information about the

View Tutorial          By: www.babescamshows.com at 2017-07-31 05:55:48

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

4. My spouse and I stumbled over here by a different website and thought I may as well check things out
View Tutorial          By: Cracking at 2017-06-16 23:17:09

5. Undeniably consider that which you stated. Your favorite justification appeared to be at the web
View Tutorial          By: ZPETS at 2017-05-21 09:49:26

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

7. Do you have any video of that? I'd love to find out
more details.

Check out m

View Tutorial          By: http://mymp3.co/Songs at 2017-04-06 07:04:45

8. Spot on with this write-up, I really feel this site needs a great deal more attention. I'll probably
View Tutorial          By: 403 ______ at 2017-04-01 16:30:22

9. Awesome post! I read your blog fairly often and you're coming out with great stuff!
I have sh

View Tutorial          By: mobile marketing at 2017-04-01 01:36:13

10. Hey thanks for your tutorial. But i'm getting an error when i run it.

Exception in th

View Tutorial          By: acc at 2013-07-25 14:30:50

11. Hey thanks for your tutorial.

But if i had a Site with a security answer, how to impl

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

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

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

14. Hi Andrew,

I have a java application which users are authentication using their crede

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

15. i want to know how to connect one page to another page program using java program i needed will you
View Tutorial          By: mahesh at 2009-08-26 01:31:40

16. Thanks for the help, however I am having some difficulty implementing it.
I keep getting the

View Tutorial          By: Andrew at 2008-09-30 14:51:14


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial