Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- import java.nio.charset.UnsupportedCharsetException;
- import java.net.Socket;
- public class Program {
- private static void readResponse(InputStream inputStream) throws Exception {
- try (
- BufferedReader in = new BufferedReader(
- new InputStreamReader(
- inputStream,
- StandardCharsets.US_ASCII
- )
- )
- ) {
- int contentLength = 0;
- String charsetName = "";
- String line = in.readLine();
- while (!line.isEmpty()) {
- System.out.println(line);
- final String[] s = line.split(":");
- if (s.length == 2) {
- if (s[0].trim().equals("Content-Length")) {
- contentLength = Integer.parseInt(s[1].trim());
- } else if (s[0].trim().equals("Transfer-Encoding")) {
- if (s[1].trim().equals("chunked")) {
- contentLength = -1;
- }
- } else if (s[0].trim().equals("Content-Type")) {
- final String[] s2 = s[1].trim().split(";");
- if (s2.length == 2) {
- final String[] s3 = s2[1].trim().split("=");
- if (s3.length == 2) {
- if (s3[0].trim().equals("charset")) {
- charsetName = s3[1].trim();
- }
- }
- }
- }
- }
- line = in.readLine();
- }
- /*if (!charsetName.isEmpty()) {
- if (Charset.isSupported(charsetName)) {
- final Charset charset = Charset.forName(charsetName);
- readBody(inputStream, charset, contentLength);
- } else {
- throw new UnsupportedCharsetException(charsetName);
- }
- } else {
- readBody(inputStream, StandardCharsets.ISO_8859_1, contentLength);
- }*/
- readBodyData(in, contentLength);
- }
- }
- private static void readBody(InputStream inputStream, Charset charset, int contentLength) throws Exception {
- try (
- BufferedReader in = new BufferedReader(
- new InputStreamReader(
- inputStream,
- charset
- )
- )
- ) {
- readBodyData(in, contentLength);
- }
- }
- private static void readBodyData(BufferedReader in, int contentLength) throws Exception {
- System.out.println();
- if (contentLength > 0) {
- final StringBuilder sb = new StringBuilder();
- boolean flagError = false;
- for (int i = 0; i < contentLength && !flagError; i++) {
- int c = in.read();
- if (c >= 0) {
- sb.append((char)c);
- } else {
- flagError = true;
- }
- }
- System.out.println(sb.toString());
- } else if (contentLength == -1) {
- final StringBuilder sb = new StringBuilder();
- boolean flagEnd = false;
- while (!flagEnd) {
- int chunkSize = 0;
- int c = in.read();
- while (c >= (int)' ') {
- if (c >= (int)'0' && c <= (int)'9') {
- chunkSize *= 16;
- chunkSize += c - (int)'0';
- } else if (c >= (int)'A' && c <= (int)'F') {
- chunkSize *= 16;
- chunkSize += c - (int)'A' + 10;
- } else if (c >= (int)'a' && c <= (int)'f') {
- chunkSize *= 16;
- chunkSize += c - (int)'a' + 10;
- } else {
- c = -1;
- }
- if (c >= 0) {
- c = in.read();
- }
- }
- flagEnd = true;
- if (c == '\r') {
- c = in.read();
- if (c == '\n') {
- boolean flagError = false;
- for (int i = 0; i < chunkSize && !flagError; i++) {
- c = in.read();
- if (c >= 0) {
- sb.append((char)c);
- } else {
- flagError = true;
- }
- }
- if (!flagError) {
- c = in.read();
- if (c == '\r') {
- c = in.read();
- if (c == '\n') {
- if (chunkSize > 0) {
- flagEnd = false;
- }
- }
- }
- }
- }
- }
- }
- System.out.println(sb.toString());
- } else {
- String line = in.readLine();
- while (line != null && !line.isEmpty()) {
- System.out.println(line);
- line = in.readLine();
- }
- }
- }
- private static void sendRequest(
- OutputStream outputStream, InputStream inputStream, String url,
- String method, String[] headers
- ) throws Exception {
- try (
- BufferedWriter out = new BufferedWriter(
- new OutputStreamWriter(
- outputStream,
- StandardCharsets.US_ASCII
- )
- )
- ) {
- out.write(method);
- out.write(' ');
- out.write(url);
- out.write(' ');
- out.write("HTTP/1.1");
- out.write('\r');
- out.write('\n');
- for (String header : headers) {
- out.write(header);
- out.write('\r');
- out.write('\n');
- }
- out.write('\r');
- out.write('\n');
- out.flush();
- readResponse(inputStream);
- }
- }
- public static void main(String[] args) {
- if (args.length != 4) {
- System.err.println("Syntax: java Program <host> <port> <method> <url>");
- System.err.println("Example: java Program www.lib.ru 80 GET /");
- System.exit(1);
- }
- try (
- Socket client = new Socket(args[0], Integer.parseInt(args[1]));
- InputStream cis = client.getInputStream();
- OutputStream cos = client.getOutputStream()
- ) {
- sendRequest(cos, cis, args[3], args[2], new String[] { "Host: " + args[0], "User-Agent: application" });
- } catch (Exception ex) {
- System.err.println("ERROR: " + ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement