471,119 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

access password protected system using java code

1
I need to write a java program in which i need to access a computer in my network using IP address but system is password protected.so how can i access a password protected system to fetch a file from specified location and use store in my system?


I got a code from google but its not working...Please help!

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.URL;

public class Main {
public static void main(String[] argv) throws Exception {
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("http://10.201.74.231:80/C:\\test\\abc.doc");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
}

class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
String promptString = getRequestingPrompt();
System.out.println(promptString);
String hostname = getRequestingHost();
System.out.println(hostname);
InetAddress ipaddr = getRequestingSite();
System.out.println(ipaddr);
int port = getRequestingPort();

String username = "priyanka";
String password = "wipro_123";
return new PasswordAuthentication(username, password.toCharArray());
}
}
Mar 24 '11 #1
0 1392

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by shoby | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.