Advertisement
Guest User

Program.java

a guest
Feb 26th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.35 KB | Source Code | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.nio.charset.Charset;
  8. import java.nio.charset.StandardCharsets;
  9. import java.nio.charset.UnsupportedCharsetException;
  10. import java.net.Socket;
  11.  
  12. public class Program {
  13.     private static void readResponse(InputStream inputStream) throws Exception {
  14.         try (
  15.             BufferedReader in = new BufferedReader(
  16.                 new InputStreamReader(
  17.                     inputStream,
  18.                     StandardCharsets.US_ASCII
  19.                 )
  20.             )
  21.         ) {
  22.             int contentLength = 0;
  23.             String charsetName = "";
  24.             String line = in.readLine();
  25.             while (!line.isEmpty()) {
  26.                 System.out.println(line);
  27.                 final String[] s = line.split(":");
  28.                 if (s.length == 2) {
  29.                     if (s[0].trim().equals("Content-Length")) {
  30.                         contentLength = Integer.parseInt(s[1].trim());
  31.                     } else if (s[0].trim().equals("Transfer-Encoding")) {
  32.                         if (s[1].trim().equals("chunked")) {
  33.                             contentLength = -1;
  34.                         }
  35.                     } else if (s[0].trim().equals("Content-Type")) {
  36.                         final String[] s2 = s[1].trim().split(";");
  37.                         if (s2.length == 2) {
  38.                             final String[] s3 = s2[1].trim().split("=");
  39.                             if (s3.length == 2) {
  40.                                 if (s3[0].trim().equals("charset")) {
  41.                                     charsetName = s3[1].trim();
  42.                                 }
  43.                             }
  44.                         }
  45.                     }
  46.                 }
  47.                 line = in.readLine();
  48.             }
  49.             /*if (!charsetName.isEmpty()) {
  50.                 if (Charset.isSupported(charsetName)) {
  51.                     final Charset charset = Charset.forName(charsetName);
  52.                     readBody(inputStream, charset, contentLength);
  53.                 } else {
  54.                     throw new UnsupportedCharsetException(charsetName);
  55.                 }
  56.             } else {
  57.                 readBody(inputStream, StandardCharsets.ISO_8859_1, contentLength);
  58.             }*/
  59.             readBodyData(in, contentLength);
  60.         }
  61.     }
  62.  
  63.     private static void readBody(InputStream inputStream, Charset charset, int contentLength) throws Exception {
  64.         try (
  65.             BufferedReader in = new BufferedReader(
  66.                 new InputStreamReader(
  67.                     inputStream,
  68.                     charset
  69.                 )
  70.             )
  71.         ) {
  72.             readBodyData(in, contentLength);
  73.         }
  74.     }
  75.  
  76.     private static void readBodyData(BufferedReader in, int contentLength) throws Exception {
  77.         System.out.println();
  78.         if (contentLength > 0) {
  79.             final StringBuilder sb = new StringBuilder();
  80.             boolean flagError = false;
  81.             for (int i = 0; i < contentLength && !flagError; i++) {
  82.                 int c = in.read();
  83.                 if (c >= 0) {
  84.                     sb.append((char)c);
  85.                 } else {
  86.                     flagError = true;
  87.                 }
  88.             }
  89.             System.out.println(sb.toString());
  90.         } else if (contentLength == -1) {
  91.             final StringBuilder sb = new StringBuilder();
  92.             boolean flagEnd = false;
  93.             while (!flagEnd) {
  94.                 int chunkSize = 0;
  95.                 int c = in.read();
  96.                 while (c >= (int)' ') {
  97.                     if (c >= (int)'0' && c <= (int)'9') {
  98.                         chunkSize *= 16;
  99.                         chunkSize += c - (int)'0';
  100.                     } else if (c >= (int)'A' && c <= (int)'F') {
  101.                         chunkSize *= 16;
  102.                         chunkSize += c - (int)'A' + 10;
  103.                     } else if (c >= (int)'a' && c <= (int)'f') {
  104.                         chunkSize *= 16;
  105.                         chunkSize += c - (int)'a' + 10;
  106.                     } else {
  107.                         c = -1;
  108.                     }
  109.                     if (c >= 0) {
  110.                         c = in.read();
  111.                     }
  112.                 }
  113.                 flagEnd = true;
  114.                 if (c == '\r') {
  115.                     c = in.read();
  116.                     if (c == '\n') {
  117.                         boolean flagError = false;
  118.                         for (int i = 0; i < chunkSize && !flagError; i++) {
  119.                             c = in.read();
  120.                             if (c >= 0) {
  121.                                 sb.append((char)c);
  122.                             } else {
  123.                                 flagError = true;
  124.                             }
  125.                         }
  126.                         if (!flagError) {
  127.                             c = in.read();
  128.                             if (c == '\r') {
  129.                                 c = in.read();
  130.                                 if (c == '\n') {
  131.                                     if (chunkSize > 0) {
  132.                                         flagEnd = false;
  133.                                     }
  134.                                 }
  135.                             }
  136.                         }
  137.                     }
  138.                 }
  139.             }
  140.             System.out.println(sb.toString());
  141.         } else {
  142.             String line = in.readLine();
  143.             while (line != null && !line.isEmpty()) {
  144.                 System.out.println(line);
  145.                 line = in.readLine();
  146.             }
  147.         }
  148.     }
  149.  
  150.     private static void sendRequest(
  151.         OutputStream outputStream, InputStream inputStream, String url,
  152.         String method, String[] headers
  153.     ) throws Exception {
  154.         try (
  155.             BufferedWriter out = new BufferedWriter(
  156.                 new OutputStreamWriter(
  157.                     outputStream,
  158.                     StandardCharsets.US_ASCII
  159.                 )
  160.             )
  161.         ) {
  162.             out.write(method);
  163.             out.write(' ');
  164.             out.write(url);
  165.             out.write(' ');
  166.             out.write("HTTP/1.1");
  167.             out.write('\r');
  168.             out.write('\n');
  169.             for (String header : headers) {
  170.                 out.write(header);
  171.                 out.write('\r');
  172.                 out.write('\n');
  173.             }
  174.             out.write('\r');
  175.             out.write('\n');
  176.             out.flush();
  177.             readResponse(inputStream);
  178.         }
  179.     }
  180.  
  181.     public static void main(String[] args) {
  182.         if (args.length != 4) {
  183.             System.err.println("Syntax: java Program <host> <port> <method> <url>");
  184.             System.err.println("Example: java Program www.lib.ru 80 GET /");
  185.             System.exit(1);
  186.         }
  187.         try (
  188.             Socket client = new Socket(args[0], Integer.parseInt(args[1]));
  189.             InputStream cis = client.getInputStream();
  190.             OutputStream cos = client.getOutputStream()
  191.         ) {
  192.             sendRequest(cos, cis, args[3], args[2], new String[] { "Host: " + args[0], "User-Agent: application" });
  193.         } catch (Exception ex) {
  194.             System.err.println("ERROR: " + ex.getMessage());
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement