Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.Socket;
- import java.net.URL;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws Exception {
- if (args.length != 1) {
- System.err.println("Pass the server IP as the sole command line argument");
- return;
- }
- try (Socket socket = new Socket(args[0], 8080)) {
- System.out.println("Enter lines of text then Ctrl+D or Ctrl+C to quit");
- Scanner scanner = new Scanner(System.in);
- Scanner in = new Scanner(socket.getInputStream());
- PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- while (scanner.hasNextLine()) {
- out.println(scanner.nextLine());
- System.out.println(in.nextLine());
- }
- }
- System.out.println(getHTML("http://www.brainjar.com/java/host/test.html"));
- }
- public static String getHTML(String urlToRead) throws Exception {
- StringBuilder result = new StringBuilder();
- URL url = new URL(urlToRead);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = rd.readLine()) != null) {
- result.append(line);
- }
- rd.close();
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement