{"id":437,"date":"2011-09-08T22:53:19","date_gmt":"2011-09-08T14:53:19","guid":{"rendered":"http:\/\/coderbee.net\/?p=437"},"modified":"2013-09-01T22:54:15","modified_gmt":"2013-09-01T14:54:15","slug":"java-socket-http","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20110908\/437","title":{"rendered":"Java Socket HTTP"},"content":{"rendered":"<p>\u672c\u6587\u6700\u65e9\u53d1\u8868\u5728ITeye\u4e0a\u7684\u4e2a\u4eba\u535a\u5ba2<a href=\"http:\/\/wen866595.iteye.com\/blog\/1168658\">http:\/\/wen866595.iteye.com\/blog\/1168658<\/a>\uff0c\u73b0\u5728\u79fb\u690d\u5230\u8fd9\u91cc\uff0c\u4e5f\u662f\u4e00\u79cd\u8bb0\u5fc6\u3002<\/p>\n<p>\u7528Java  Socket \u5b9e\u73b0\u53d1\u9001HTTP GET\u8bf7\u6c42\u548c\u8bfb\u53d6\u54cd\u5e94\u3002<\/p>\n<pre><code>\npublic class LearnHttp {\n    private static final byte CR = '\\r';\n    private static final byte LF = '\\n';\n    private static final byte[] CRLF = {CR, LF};\n\n    public static void main(String[] args) throws UnknownHostException, IOException {\n        new LearnHttp().testHttp();\n    }\n    \n    public void testHttp() throws UnknownHostException, IOException {\n        String host = \"www.baidu.com\";\n        Socket socket = new Socket(host, 80);\n        \n        OutputStream out = socket.getOutputStream();\n        InputStream in = socket.getInputStream();\n        \n        \/\/ \u5728\u540c\u4e00\u4e2aTCP\u8fde\u63a5\u91cc\u53d1\u9001\u591a\u4e2aHTTP\u8bf7\u6c42\n        for(int i = 0; i < 2; i++) {\n            writeRequest(out, host);\n            readResponse(in);\n            System.out.println(\"\\n\\n\\n\");\n        }\n    }\n    \n    private void writeRequest(OutputStream out, String host) throws IOException {\n        \/\/ \u8bf7\u6c42\u884c\n        out.write(\"GET \/index.html HTTP\/1.1\".getBytes());\n        out.write(CRLF);        \/\/ \u8bf7\u6c42\u5934\u7684\u6bcf\u4e00\u884c\u90fd\u662f\u4ee5CRLF\u7ed3\u5c3e\u7684\n        \n        \/\/ \u8bf7\u6c42\u5934\n        out.write((\"Host: \" + host).getBytes()); \/\/ \u6b64\u8bf7\u6c42\u5934\u5fc5\u987b\n        out.write(CRLF);\n\n        out.write(CRLF);        \/\/ \u5355\u72ec\u7684\u4e00\u884cCRLF\u8868\u793a\u8bf7\u6c42\u5934\u7684\u7ed3\u675f\n\n        \/\/ \u53ef\u9009\u7684\u8bf7\u6c42\u4f53\u3002GET\u65b9\u6cd5\u6ca1\u6709\u8bf7\u6c42\u4f53\n        \n        out.flush();\n    }\n\n    private void readResponse(InputStream in) throws IOException {\n        \/\/ \u8bfb\u53d6\u72b6\u6001\u884c\n        String statusLine = readStatusLine(in);\n        System.out.println(\"statusLine :\" + statusLine);\n        \n        \/\/ \u6d88\u606f\u62a5\u5934\n        Map<String, String> headers = readHeaders(in);\n        \n        int contentLength = Integer.valueOf(headers.get(\"Content-Length\"));\n        \n        \/\/ \u53ef\u9009\u7684\u54cd\u5e94\u6b63\u6587\n        byte[] body = readResponseBody(in, contentLength);\n        \n        String charset = headers.get(\"Content-Type\");\n        if(charset.matches(\".+;charset=.+\")) {\n            charset = charset.split(\";\")[1].split(\"=\")[1];\n        } else {\n            charset = \"ISO-8859-1\";     \/\/ \u9ed8\u8ba4\u7f16\u7801\n        }\n        \n        System.out.println(\"content:\\n\" + new String(body, charset));\n    }\n    \n    private byte[] readResponseBody(InputStream in, int contentLength) throws IOException {\n        \n        ByteArrayOutputStream buff = new ByteArrayOutputStream(contentLength);\n        \n        int b;\n        int count = 0;\n        while(count++ < contentLength) {\n            b = in.read();\n            buff.write(b);\n        }\n        \n        return buff.toByteArray();\n    }\n    \n    private Map<String, String> readHeaders(InputStream in) throws IOException {\n        Map<String, String> headers = new HashMap<String, String>();\n        \n        String line;\n        \n        while(!(\"\".equals(line = readLine(in)))) {\n            String[] nv = line.split(\": \");     \/\/ \u5934\u90e8\u5b57\u6bb5\u7684\u540d\u503c\u90fd\u662f\u4ee5(\u5192\u53f7+\u7a7a\u683c)\u5206\u9694\u7684\n            headers.put(nv[0], nv[1]);\n        }\n        \n        return headers;\n    }\n    \n    private String readStatusLine(InputStream in) throws IOException {\n        return readLine(in);\n    }\n    \n    \/**\n     * \u8bfb\u53d6\u4ee5CRLF\u5206\u9694\u7684\u4e00\u884c\uff0c\u8fd4\u56de\u7ed3\u679c\u4e0d\u5305\u542bCRLF\n     *\/\n    private String readLine(InputStream in) throws IOException {\n        int b;\n        \n        ByteArrayOutputStream buff = new ByteArrayOutputStream();\n        while((b = in.read()) != CR) {\n            buff.write(b);\n        }\n        \n        in.read();      \/\/ \u8bfb\u53d6 LF\n        \n        String line = buff.toString();\n        \n        return line;\n    }\n    \n}\n<\/code><\/pre>\n<p>\u503c\u5f97\u8bb0\u4e0b\u7684\u6559\u8bad\uff1a<\/p>\n<p>InputStream.read()\u8fd4\u56de\uff0d1\u8868\u793a\u6d41\u7684\u7ed3\u675f\uff0c\u5373\u6d41\u88ab\u5173\u95ed\u4e86\u3002\u5728\u8fd9\u6b21\u5b9e\u73b0\u540c\u4e00\u4e2aSocket\u53d1\u9001\u591a\u4e2aHTTP\u8bf7\u6c42\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e00\u5f00\u59cb\u603b\u662f\u4ee5\u4e3a\u4e00\u4e2a\u54cd\u5e94\u62a5\u6587\u7684\u7ed3\u675f\u662f\u5728InputStream.read\u8fd4\u56de\uff0d1\u65f6\uff0c\u5bfc\u81f4\u7b2c\u4e00\u6b21\u8bfb\u53d6\u54cd\u5e94\u65f6\u603b\u662f\u5f88\u4e45\u624d\u7ed3\u675f\uff0c\u7b2c\u4e8c\u6b21\u7684\u8bf7\u6c42\u867d\u7136\u53d1\u51fa\u53bb\u4e86\uff0c\u4f46\u54cd\u5e94\u603b\u662f\u7a7a\u7684\u3002<\/p>\n<p>\u8bfb\u53d6\u7b2c\u4e00\u6b21\u8bf7\u6c42\u7684\u54cd\u5e94\u8981\u5f88\u4e45\u662f\u56e0\u4e3a\uff0c\u8fd9\u662f\u5728\u7b49\u5f85\u670d\u52a1\u5668\u5173\u95ed\u8fde\u63a5\uff0c\u767e\u5ea6\u7684\u57fa\u672c\u4e0a\u662f60\u79d2\u3002\u7b2c\u4e8c\u6b21\u662f\u7a7a\u7684\u662f\u56e0\u4e3a\uff0c\u8bf7\u6c42\u867d\u7136\u53d1\u51fa\u53bb\u4e86\uff0c\u4f46\u670d\u52a1\u5668\u5199\u54cd\u5e94\u7684\u6d41\u5df2\u7ecf\u88ab\u5173\u95ed\uff0c\u6240\u4ee5\u4e5f\u8bfb\u4e0d\u5230\u54cd\u5e94\u3002<\/p>\n<p>\u5bfc\u81f4\u8fd9\u4e2a\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u5728\u4e8e\u6ca1\u6709\u8bb0\u4f4f\uff1aTCP\u662f\u9762\u5411\u6d41\u7684\uff0c\u5e76\u4e0d\u77e5\u9053\u4f20\u8f93\u7684\u6d88\u606f\u7684\u5f00\u59cb\u548c\u7ed3\u675f\uff0c\u8fd9\u662f\u7531\u4e0a\u5c42\u7684\u534f\u8bae\u53bb\u63a7\u5236\u7684\uff0c\u8fd9\u91cc\u662fHTTP\u534f\u8bae\u3002<\/p>\n<p>2013-09-01\uff1a\u8fd9\u662f\u5f53\u65f6\u5c1d\u8bd5HTTP pipeline\uff0c\u7531\u4e8e\u767e\u5ea6\u670d\u52a1\u5668\u4e0d\u652f\u6301\u800c\u5931\u8d25\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6700\u65e9\u53d1\u8868\u5728ITeye\u4e0a\u7684\u4e2a\u4eba\u535a\u5ba2http:\/\/wen866595.iteye &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20110908\/437\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[86,87],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/437"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/437\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}