4

I use simple code to get html for http://www.ip-adress.com, but it shows error http code 403. I try it in other website like google.com in program, it can work. i can also open www.ip-adress.com in browse, why i can't use it in java program.

 public class urlconnection
{
  public static void main(String[] args)
 {
    StringBuffer document = new StringBuffer();
    try 
    {
        URL url = new URL("http://www.ip-adress.com");
        URLConnection conn = url.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null)
        document.append(line + " ");
        reader.close();
    }
    catch (MalformedURLException e) 
    {
        e.printStackTrace(); 
    }
    catch (IOException e)
    {
        e.printStackTrace(); 
    }
    System.out.println(document.toString());
}
}



java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.ip-adress.com/

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at urlconnection.main(urlconnection.java:14)
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
AntiGMO
  • 1,535
  • 5
  • 23
  • 38

5 Answers5

3

This is the line you required

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

refer this

Community
  • 1
  • 1
someone
  • 6,577
  • 7
  • 37
  • 60
1

The web-server can detect that you are not actually trying to access it via HTTP, so it rejects your request. There are ways to fake that to trick the server into thinking that you are a browser.

hesson
  • 1,812
  • 4
  • 23
  • 35
1

I suppose the site checks user agent header and blocks what it seems to be "a robot". You need to mimic normal browser. Check this solution Setting user agent of a java URLConnection or try to use commons http client AND set user agent.

Community
  • 1
  • 1
xeye
  • 1,250
  • 10
  • 15
1

I don't believe that this is fundamentally a Java problem. You're doing the right thing to make an HTTP connection, and the server is doing "the right thing" from its perspective by responding to your request with a 403 response.

Let's be clear about this - the response you're getting is due to whatever logic is being employed by the target webserver.

So if you were to ask "how can I modify my request so that http://www.ip-address.com returns a 200 response", then people may be able to come up with workarounds that keep that server happy. But this is a host-specific process; your Java code is arguably correct, though it should have better error handling because you can always get non-2xx responses.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
0

Try to change Connection User-Agent to something like Browsers, most of times I use Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77