Java HTTP Port Find readHttpContent(String domain, String resource, int port)

Here you can find the source of readHttpContent(String domain, String resource, int port)

Description

read Http Content

License

Open Source License

Declaration

public static final List<String> readHttpContent(String domain, String resource, int port) 

Method Source Code


//package com.java2s;
/*/*from   w w  w.ja  va  2s  . c  o  m*/
 * Copyright 1999-2004 Alibaba.com All right reserved. This software is the confidential and proprietary information of
 * Alibaba.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Alibaba.com.
 */

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

    public static final List<String> readHttpContent(String domain, String resource, int port) {
        Socket socket = null;
        try {
            socket = new Socket(domain, port);

            PrintWriter out = new PrintWriter(socket.getOutputStream());
            StringBuilder send = new StringBuilder();
            send.append("GET " + resource + " HTTP/1.0").append("\n");
            send.append("Accept: text/html").append("\n");
            send.append("\n");
            out.write(send.toString());
            out.flush();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = null;
            List<String> result = new ArrayList<String>();
            while ((line = br.readLine()) != null) {
                if (line.contains("HTTP/1.1 5")) {
                    return Collections.emptyList();
                }

                result.add(line);
            }

            return result;
        } catch (Exception ex) {
            return Collections.emptyList();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception ex) {

                }
            }
        }
    }
}

Related

  1. randomAvailablePort()
  2. randomFreePort()
  3. randomFreePort()
  4. randomFreePort()
  5. read(String host, int port)
  6. replace(final String host, final int port, final String user, final String program, final String relation, final String dataset, final String format)
  7. resolve(final String desc, final int defaultPort)
  8. safePort(final String port)
  9. searchPort(int number, int even, boolean stream)