permaculture playing cards*
The moose likes Java in General and the fly likes Thro a proxy server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thro a proxy server" Watch "Thro a proxy server" New topic
Author

Thro a proxy server

Guhan Ramanan
Greenhorn

Joined: Jun 29, 2000
Posts: 7
Hello people!
At my place of work, my machine is connected to the Net thro a proxy server.
When I run any Internet programs using methods like
getByName(), it says host not found. How do I get to the Net using a proxy?
The sample code follows:
import java.io.*;
import java.net.*;
class InetTest
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("www.hotmail.com");
System.out.println(Address);
}
}

Plz let me know
-Guhan

<B> Guhan Ramanan </B><BR>Sun certified Programmer for Java2 Platform
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
In the main method just type these two lines.
Properties prop=System.getProperties();
prop.put("http.proxyHost",<your proxy host name> );
prop.put("http.proxyPort",<your proxy port no> );
remember replace "your proxy host name" with proper host name and "your proxy port no" with proper port no.
I think this should work fine.
Loke.
Vim Win
Greenhorn

Joined: May 01, 2004
Posts: 21
Hi lokesh,
I have the same setup in which I connect to a Proxy which is connected to Internet. I tried the same example given above with Properties being set. But I was not able to connect and it gave me a UnknownHostException. Can you clarify what could be the reason ??
The code is as follows :
Properties prop = System.getProperties();
prop.put("http.proxyHost","172.16.0.104");
prop.put("http.proxyPort","80");
Then I tried the Hotmail name. I couldnt get the IP.
Moreover is there anyway by which we can get the name of the host given its IP address. Say, I have to know the name of the proxy. I know the IP as 172.16.0.104. With this info, can I get the name of the Server ???
Kindly clarify.
With Advance Thanx.
JVRN.
Guhan Ramanan
Greenhorn

Joined: Jun 29, 2000
Posts: 7
Hi JVRN
u can get the name of the server by passing the IP as a String.
btw, I tried the following code and it still doesnt work..
import java.io.*;
import java.net.*;
import java.util.*;
class InetTest
{
public static void main(String args[]) throws UnknownHostException
{
Properties prop=System.getProperties();
prop.put("http.proxyHost","PROXYSERVER");
prop.put("http.proxyPort","80");

InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("www.hotmail.com");
System.out.println(Address);
}
}

It displays my local host but not hotmail's IP.
I tried lower and Upper Cases with http.
My system property gets added with http.ProxyServer="PROXYSERVER" and the port also.
I am still unable to get out of the proxyserver
Plz help
-Guhan
Rajamohan Thangapalam
Greenhorn

Joined: Oct 02, 2005
Posts: 1
Dear,

The About code for proxy work fine for the http request only not DNS for your code to work you should set the proper DNS

bye
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thro a proxy server