Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import com.sun.deploy.util.StringUtils;
  2. import org.json.simple.JSONArray;
  3. import org.json.simple.JSONObject;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.Reader;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.util.ArrayList;
  12.  
  13.  
  14. public class GetNeighbors {
  15.     public static String myIp = "7000";
  16.     public ArrayList<String> neighborgIps = new ArrayList<>();
  17.  
  18.     public static void main(String[] args) {
  19.         runr();
  20.     }
  21.  
  22.     public static void runr() {
  23.         try {
  24.             while (true) {
  25.                 makeRequestToServer();
  26.                 Thread.sleep(60 * 1000);
  27.             }
  28.         } catch (InterruptedException | IOException e) {
  29.             e.printStackTrace();
  30.         }
  31.     }
  32.  
  33.  
  34.     public static void makeRequestToServer() throws IOException {
  35.         URL url = new URL("http://localhost:9000");
  36.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  37.         conn.setRequestMethod("POST");
  38.         String body = "{\"ip\":\"" + myIp + "\",\"action\":\"Enter\"}";
  39.         conn.setRequestMethod("POST");
  40.         conn.setRequestProperty("Content-Type", "text/plain");
  41.         conn.setRequestProperty("Content-Length", Integer.toString(body.length()));
  42.         conn.setDoOutput(true);
  43.         conn.getOutputStream().write(body.getBytes("UTF8"));
  44.  
  45.         Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  46.  
  47.         StringBuilder str = new StringBuilder();
  48.         for (int c; (c = in.read()) >= 0;) {
  49.             System.out.print((char)c);
  50.             str.append((char)c);
  51.         }
  52.         String answer = str.toString();
  53.         System.out.println(answer);
  54.        
  55.  
  56.  
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement