Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-05-2008, 04:18 PM
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);
}


}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-08-2008, 02:42 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 220
DonCash will become famous soon enoughDonCash will become famous soon enough
Hello asheesh,

Welcome to the Java Forums.

It seems to me this is because the sites are blocking connectings from programs that are not web browsers. What you need to do is to trick these sites into thinking your program is a web browser.

Try this:

Code:
import java.net.*; import java.io.*; class httpconnect { public static void main(String[] args) throws Exception { URL url = new URL("http://www.anywebsite.com/"); URLConnection spoof = url.openConnection(); spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" ); BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream())); String strLine = ""; while ((strLine = in.readLine()) != null) { System.out.println(strLine); } in.close(); } }
Note this bit of code:

Code:
spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
This is what tricks the sites into thinking you are using a web browser.

I'm sure this will fix your problem!
__________________
Did this post help you? Please me! || Don't forget to: Mark your Thread as Solved
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does OS intervene when reading Java text files Tina G Advanced Java 1 04-07-2008 03:29 PM
reading dir in java applets willemjav Java Applets 3 02-07-2008 01:36 AM
reading textfile from java problem saytri New To Java 1 01-17-2008 03:13 AM
Question abt.reading xml file using java gvi Advanced Java 6 11-08-2007 06:48 PM
Help with IRC server in java mathias Networking 1 08-07-2007 07:51 AM


All times are GMT +3. The time now is 06:21 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org