View Single Post
  #1 (permalink)  
Old 04-05-2008, 04:18 PM
asheesh asheesh is offline
Member
 
Join Date: Apr 2008
Posts: 1
asheesh is on a distinguished road
reading URL using java through proxy server
hi all
i made a java application through user can access webpage through proxy server, but this works for only some pages like (http://www.iita-conference.org,yahoo.com,iiitk.ac.in etc) it not works for like rediff.com, ieeeexplore,orkut etc. i also post the code plz tell me where is the problem? why it not works for all URLs?
if possible plz send code too.

package ieee;

import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author sknavin
*/
public class Main {

/**
* @param args the command line arguments
*/
private static String loadPage(URL parURL) throws Exception {

Properties sysProps = System.getProperties();
sysProps.put("proxySet", "true");
sysProps.put("proxyHost", "172.31.1.3");
sysProps.put("proxyPort", "8080");

Authenticator authenticator = new Authenticator() {

public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("userID","password".toCharA rray()));
}
};
Authenticator.setDefault(authenticator);

long lTime = System.currentTimeMillis();
StringBuffer sb = new StringBuffer("");
String http = parURL.toString();
// Create a connection.
HttpURLConnection urlConnection =
(HttpURLConnection) parURL.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("USER-AGENT", "Mozilla/2.02Gold (WinNT; I)");
//
urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlenCcoded");
urlConnection.setAllowUserInteraction(true);

urlConnection.connect();

InputStream in =
((HttpURLConnection) urlConnection).getInputStream();
int length = urlConnection.getContentLength();
for (int n = 0; n < length; n++) {
sb.append((char) in.read());
}

return sb.toString();
}

public static void main(String[] args) {
try {
// type your intended url here
URL url = new URL("http://www.iita-conference.org/");
String s = loadPage(url);
System.out.println("S=" + s);


} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.S EVERE, null, ex);
}


}
}
Reply With Quote
Sponsored Links