fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8.  
  9. import javax.net.ssl.HttpsURLConnection;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. String url = "http://www.google.com/search?q=mkyong";
  17.  
  18. URL obj = new URL(url);
  19. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  20.  
  21. // optional default is GET
  22. con.setRequestMethod("GET");
  23.  
  24. //add request header
  25. con.setRequestProperty("User-Agent", USER_AGENT);
  26.  
  27. int responseCode = con.getResponseCode();
  28. System.out.println("\nSending 'GET' request to URL : " + url);
  29. System.out.println("Response Code : " + responseCode);
  30.  
  31. new InputStreamReader(con.getInputStream()));
  32. String inputLine;
  33. StringBuffer response = new StringBuffer();
  34.  
  35. while ((inputLine = in.readLine()) != null) {
  36. response.append(inputLine);
  37. }
  38. in.close();
  39.  
  40. //print result
  41. System.out.println(response.toString());
  42. }
  43. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:25: error: cannot find symbol
		con.setRequestProperty("User-Agent", USER_AGENT);
		                                     ^
  symbol:   variable USER_AGENT
  location: class Ideone
1 error
stdout
Standard output is empty