Guest User

Untitled

a guest
Jun 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. public static void main(String[] args) {
  2. String query = "";
  3.  
  4.  
  5.  
  6. HttpURLConnection connection = null;
  7. try {
  8.  
  9. connection = (HttpURLConnection) new URL(query).openConnection();
  10.  
  11.  
  12. connection.setRequestMethod("GET");
  13.  
  14.  
  15.  
  16. connection.setUseCaches(false);
  17. connection.setConnectTimeout(250);
  18. connection.setConnectTimeout(250);
  19.  
  20. connection.connect();
  21.  
  22. StringBuilder sb = new StringBuilder();
  23.  
  24.  
  25.  
  26. if(HttpURLConnection.HTTP_OK == connection.getResponseCode()){
  27. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  28.  
  29. String line;
  30. while ((line = in.readLine()) !=null) {
  31. sb.append(line);
  32. sb.append("n");
  33. }
  34. System.out.println(sb.toString());
  35. }else{
  36. System.out.println("fail :" + connection.getResponseCode());
  37. }
  38. }catch (Throwable cause){
  39. cause.printStackTrace();
  40. }finally {
  41. if (connection != null){
  42. connection.disconnect();
  43. }
  44. }
  45. }
Add Comment
Please, Sign In to add comment