There is such a code
import java.io.*; import java.net.Socket; import java.text.ParseException; import static java.lang.Thread.sleep; public class HttpClient { public static void main(String[] args) { try { String header = null; if (args.length == 0) { header = readHeader(System.in); } else { FileInputStream fis = new FileInputStream(args[0]); header = readHeader(fis); fis.close(); } //System.out.println("Заголовок: \n" + header); /* Запрос отправляется на сервер и мы получаем ответ*/ String answer = sendRequest(header); /* Ответ выводится на консоль */ System.out.println("Ответ от сервера: \n"); System.out.write(answer.getBytes()); } catch (Exception e) { System.err.println(e.getMessage()); e.getCause().printStackTrace(); } } /** * Читает поток и возвращает его содержимое в виде строки. */ public static String readHeader(InputStream strm) throws IOException { byte[] buff = new byte[64 * 1024]; int length = strm.read(buff); String res = new String(buff, 0, length); return res; } /** * Отправляет запрос в соответствии с Http заголовком. * * @return ответ от сервера. */ public static String sendRequest(String httpHeader) throws Exception { /* Из http заголовка берется арес сервера */ String host = null; int port = 0; try { host = getHost(httpHeader); port = getPort(host); host = getHostWithoutPort(host); } catch (Exception e) { throw new Exception("Не удалось получить адрес сервера.", e); } /* Отправляется запрос на сервер */ Socket socket; try { socket = new Socket(host, port); System.out.println("Создан сокет: " + host + " port:" + port); OutputStream os = socket.getOutputStream(); os.write(httpHeader.getBytes()); os.flush(); System.out.println("Заголовок отправлен. \n"); } catch (Exception e) { throw new Exception("Ошибка при отправке запроса: " + e.getMessage(), e); } /* Ответ от сервера записывается в результирующую строку */ String res = null; try { BufferedReader bfr = new BufferedReader(new InputStreamReader(socket.getInputStream())); StringBuffer sbf = new StringBuffer(); int ch = bfr.read(); while (ch != -1) { sbf.append((char) ch); ch = bfr.read(); } res = sbf.toString(); } catch (Exception e) { throw new Exception("Ошибка при чтении ответа от сервера.", e); } socket.close(); return res; } /** * Возвращает имя хоста (при наличии порта, с портом) из http заголовка. */ private static String getHost(String header) throws ParseException { final String host = "Host: "; final String normalEnd = "\n"; final String msEnd = "\r\n"; int s = header.indexOf(host, 0); if (s < 0) { return "localhost"; } s += host.length(); int e = header.indexOf(normalEnd, s); e = (e > 0) ? e : header.indexOf(msEnd, s); if (e < 0) { throw new ParseException( "В заголовке запроса не найдено " + "закрывающих символов после пункта Host.", 0); } String res = header.substring(s, e).trim(); return res; } /** * Возвращает номер порта. */ private static int getPort(String hostWithPort) { int port = hostWithPort.indexOf(":", 0); port = (port < 0) ? 80 : Integer.parseInt(hostWithPort .substring(port + 1)); return port; } /** * Возвращает имя хоста без порта. */ private static String getHostWithoutPort(String hostWithPort) { int portPosition = hostWithPort.indexOf(":", 0); if (portPosition < 0) { return hostWithPort; } else { return hostWithPort.substring(0, portPosition); } } } Title:
HEAD http://www.w3.org/standvgbuards/webdesign/htmlcss HTTP/1.1 Host: www.w3.org Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */* Referer: http://localhost/ Accept-Language: ru User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705) An empty string comes from the server. Why?