Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help in Netowork Programming.  RSS feed

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please look at the following code:

import java.net.*;
import java.io.*;

public class WebReader {
public static void main(String[] args) {
try {
java.util.Properties systemProperties = System.getProperties();
systemProperties.put("http.proxyHost","202.223.5.25");
systemProperties.put("http.proxyPort","4001");
Authenticator urlAuthenticator = new ProxyAuthenticator();
Authenticator.setDefault(urlAuthenticator);
Socket socket = new Socket("time.nist.gov", 13);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
reader.readLine(); // skip blank line
String message = reader.readLine();
System.out.println("WATCHOUT THIS "+message);
}catch(Exception e) {
e.printStackTrace();
}
}
}

class ProxyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("userName","password".toCharArray());
}
}

While executing i am getting Connection Time out Exception.

But if I use URLs the program works fine. Since i am inside proxy server i have implemented my own Authenticator so that it works fine and downloads the data from the Web when i use URLs but the same thing does not work when i use Sockets. What is the reason for this and how to overcome this? Please explain.

Thanks in Advance!!!
dinesh.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connecting over port 13 to a time server does not use HTTP, so anything you do with HTTP proxies and authentication is not applicable. Rewrite the code to use HttpUrlConnection (instead of raw TCP/IP socket connections) and an HTTP server.
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! But please clarify this also.
Does it mean that we wont be able to establish a Socket connection to a web server and there by to retrieve the data from it? I have read in some networking articles about this. Please help me.

that is cant I use a code something like this:

Socket s = new Socket("www.google.com",80);

Given that, I have established my own Authenticator to provide the Proxy username and password credentials.

Thanks in Advance!!!
dinesh.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can absolutely do that, but what you send and receive over that socket is HTTP, which you need to emulate. Which begs the question, why not use HttpUrlConnection -which knows all about HTTP- instead of reinventing the wheel?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!