title: 403 Forbidden with Java but not web browser?
body: <p>I am writing a small Java program to get the amount of results for a given Google search term. For some reason, in Java I am getting a 403 Forbidden but I am getting the right results in web browsers. Code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.URL;
  
  
  public class DataGetter {
  
      public static void main(String[] args) throws IOException {
          getResultAmount("test");
      }
  
      private static int getResultAmount(String query) throws IOException {
          BufferedReader r = new BufferedReader(new InputStreamReader(new URL("https://www.google.com/search?q=" + query).openConnection()
                  .getInputStream()));
          String line;
          String src = "";
          while ((line = r.readLine()) != null) {
              src += line;
          }
          System.out.println(src);
          return 1;
      }
  
  }
  </code></pre>
  
  <p>And the error:</p>
  
  <pre><code>Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.google.com/search?q=test
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
      at DataGetter.getResultAmount(DataGetter.java:15)
      at DataGetter.main(DataGetter.java:10)
  </code></pre>
  
  <p>Why is it doing this?</p>