0

im trying to write a client server program, in my program, each turn, client sends two String to the server, first one is a web address and second one is some random information about user.but when the url address arrives at server, i get this exception:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.com.hk/search?hl=en&source=hp&q=java&gbv=2
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
    at java.net.URL.openStream(URL.java:1009)
    at server.Server.processClient(Dom.java:124)
    at server.Server.run(Dom.java:90)
    at server.Server.main(Dom.java:155)
Java Result: 1

but i have no idea why getting a forbidden response.

Client side

              private static BufferedWriter toServer;
....

  public void progressUpdated(NavigatorProgressEvent npe) {



            Matcher matcher = pattern.matcher (npe.getUrl().toString());


            if (matcher.matches ())
                        {
                System.out.println(npe.getUrl());
                        toServer.println(npe.getUrl().toString());


                                }

Server side

...
   url = new URL(fromClient.readLine());

....

lonesome
  • 2,503
  • 6
  • 35
  • 61

1 Answers1

0

Maybe you miss "User-Agent" in your headers request.

Try with adding this code:

httpUrlConnection.setRequestProperty("User-Agent","MyUserAgentName");

Google do not respond if you don't specify a user agent name. And try to choose a good user agent name :D

dash1e
  • 7,677
  • 1
  • 30
  • 35