e140. getting the response headers from an http connection.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 38 行

TXT
38
字号
try {
        // Create a URLConnection object for a URL
        URL url = new URL("http://hostname:80");
        URLConnection conn = url.openConnection();
    
        // List all the response headers from the server.
        // Note: The first call to getHeaderFieldKey() will implicit send
        // the HTTP request to the server.
        for (int i=0; ; i++) {
            String headerName = conn.getHeaderFieldKey(i);
            String headerValue = conn.getHeaderField(i);
    
            if (headerName == null && headerValue == null) {
                // No more headers
                break;
            }
            if (headerName == null) {
                // The header value contains the server's HTTP version
            }
        }
    } catch (Exception e) {
    }

Here's a sample of headers from a website: 
    Key=Value
    
    null=HTTP/1.1 200 OK
    Server=Netscape-Enterprise/4.1
    Date=Mon, 11 Feb 2002 09:23:26 GMT
    Cache-control=public
    Content-type=text/html
    Etag="9fa67d2a-58-71-3bbdad3283"
    Last-modified=Fri, 05 Oct 2001 12:53:06 GMT
    Content-length=115
    Accept-ranges=bytes
    Connection=close

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?