The Artima Developer Community
Sponsored Link

Java Answers Forum
Network Authentication

4 replies on 1 page. Most recent reply: Jun 16, 2004 11:34 PM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Network Authentication Posted: Jun 13, 2004 1:19 PM
Reply to this message Reply
Advertisement
Hi

I’m creating a small application which I want to tell me the weather in my country and area. It needs to fetch a picture from the Internet. When I connect to web sites outside of my network, I need to enter user name and password. How do I tell the server what my user name and password is? This code works fine if I get a picture from my local intranet, as this doesn’t require authentication.
import javax.swing.*;
import java.io.*;
import java.net.*;
 
public class Weather extends JFrame
{	
	public static void main (String[] args) throws IOException
	{
		JFrame wFrame = new JFrame ("Easter Cape Weather");
		wFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		
		URL page = new URL ("http://www.sabcnews.com/weathermaps/cloud.jpeg");
		ImageIcon pic = new ImageIcon(page);
		
		wFrame.getContentPane().add (new WeatherPanel (pic));
		
		wFrame.pack();
		wFrame.show();
	}	
}
 
 
import javax.swing.*;
import java.awt.*;
 
public class WeatherPanel extends JPanel
{
	private ImageIcon pic;
	
	public WeatherPanel (ImageIcon pic)
	{
		this.pic = pic;
		setPreferredSize (new Dimension (pic.getIconWidth(), pic.getIconHeight()));
	}
	
	public void paintComponent (Graphics g)
	{
		super.paintComponent(g);
		pic.paintIcon (this, g, 0, 0);
	}
}


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Network Authentication Posted: Jun 13, 2004 9:25 PM
Reply to this message Reply
Do u mean the password user name of ur network?

The firewall proxy password?

If that is the case u need to set ur proxy password.

Check this
http://www.javaworld.com/javaworld/javatips/jw-javatip42.html

Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Re: Network Authentication Posted: Jun 13, 2004 11:14 PM
Reply to this message Reply
Thanks. The link looks useful. It's a proxy password that is required.

Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Re: Network Authentication Posted: Jun 16, 2004 11:20 PM
Reply to this message Reply
I had a look at that link, but it didn’t really help me. This is how I solved the problem. I added these four lines of code to the beginning of my main program.

Properties props = System.getProperties();
props.put("http.proxyHost", "xxx");
props.put("http.proxyPort", "xxx");
Authenticator.setDefault(new FrodoAuth());
 
//*****************************************************
import java.net.*;
 
public class FrodoAuth extends Authenticator
{
	protected PasswordAuthentication getPasswordAuthentication()
	{
		String password = "password";
	
		return new PasswordAuthentication("username", password.toCharArray());
	}
}

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Network Authentication Posted: Jun 16, 2004 11:34 PM
Reply to this message Reply
I tried the following code and it worked for me
HttpURLConnection httpCon = null;
URL url = new URL("http://www.website.com");
  	System.setProperty("http.proxyHost","proxy.host1.host2.com"); 
System.setProperty("http.proxyPort","80"); 
Authenticator.setDefault (new MyAuthenticator ());
 
httpCon.setUseCaches(false);
httpCon.setDoOutput (true);
httpCon.setDoInput(true);
		   
httpCon.connect();
    
int responseCode = httpCon.getResponseCode();
 
//and the class
 
class MyAuthenticator extends Authenticator {
 
protected PasswordAuthentication getPasswordAuthentication() { 
	
String userName = PropertiesHelper.getProperty("proxy.userName");
String password = PropertiesHelper.getProperty("proxy.password"); 
 
return new PasswordAuthentication (userName, password.toCharArray()); 
} 
 
 


and it worked fine for me

Flat View: This topic has 4 replies on 1 page
Topic: About jni Previous Topic   Next Topic Topic: Help Please

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use