問題描述
有沒有辦法獲取原始響應 http 標頭?
getHeaderField()
方法對我不起作用,因為服務器吐出多個Set-Cookie",其中一些會丟失.
getHeaderField()
方法對我不起作用
您是在 <代碼>java.net.URLConnection,是嗎?不,使用 URLconnection
無法獲取原始 HTTP 響應標頭.您需要退回到低級別的 Socket 編程.這是一個SSCCE,只需復制'n'paste'n'運行它.
包com.stackoverflow.q2307291;導入 java.io.BufferedReader;導入 java.io.IOException;導入 java.io.InputStreamReader;導入 java.io.OutputStreamWriter;導入 java.io.PrintWriter;導入 java.net.Socket;公共類測試{公共靜態 void main(String[] args) 拋出 IOException {字符串主機名 = stackoverflow.com";國際端口 = 80;套接字套接字 = null;PrintWriter 作家 = null;BufferedReader 閱讀器 = null;嘗試 {套接字 = 新套接字(主機名,端口);writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));writer.println("GET/HTTP/1.1");writer.println("主機:" + 主機名);writer.println("接受:*/*");writer.println("用戶代理:Java");//說實話.writer.println("");//重要,否則服務器會期望請求中包含更多內容.writer.flush();reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));for (String line; (line = reader.readLine()) != null;) {if (line.isEmpty()) 中斷;//當標題完成時停止.我們對所有的 HTML 不感興趣.System.out.println(line);}} 最后 {if (reader != null) try { reader.close();} 捕捉(IOException logOrIgnore){}if (writer != null) { writer.close();}if (socket != null) try { socket.close();} 捕捉(IOException logOrIgnore){}}}}
為避免每個嘗試此代碼段的人都使 SO 過載,輸出如下所示:
<上一頁>HTTP/1.1 200 正常緩存控制:私有內容類型:文本/html;字符集=utf-8過期:2010 年 2 月 21 日星期日 20:39:08 GMT服務器:Microsoft-IIS/7.5日期:2010 年 2 月 21 日星期日 20:39:07 GMT連接:關閉內容長度:208969要了解有關以低級方式發送 HTTP 請求的更多信息,請閱讀 HTTP 規范.
但是,您可能想使用 getHeaderFields()
方法來檢索具有多個值的標頭.<代碼>getHeaderField() 即只返回最后一個值,根據鏈接的 API 文檔.
列表<字符串>cookies = connection.getHeaderFields().get("Set-Cookie");
Is there any way to get raw response http header?
The getHeaderField()
method doesn't work for me, because server spits multiple 'Set-Cookie' and some of them get lost.
The
getHeaderField()
method doesn't work for me
You're asking this in the context of java.net.URLConnection
, is it? No, obtaining the raw HTTP response headers is not possible with URLconnection
. You'll need to fall back to low-level Socket programming. Here's an SSCCE, just copy'n'paste'n'run it.
package com.stackoverflow.q2307291;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class Test {
public static void main(String[] args) throws IOException {
String hostname = "stackoverflow.com";
int port = 80;
Socket socket = null;
PrintWriter writer = null;
BufferedReader reader = null;
try {
socket = new Socket(hostname, port);
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.println("GET / HTTP/1.1");
writer.println("Host: " + hostname);
writer.println("Accept: */*");
writer.println("User-Agent: Java"); // Be honest.
writer.println(""); // Important, else the server will expect that there's more into the request.
writer.flush();
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
for (String line; (line = reader.readLine()) != null;) {
if (line.isEmpty()) break; // Stop when headers are completed. We're not interested in all the HTML.
System.out.println(line);
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
if (writer != null) { writer.close(); }
if (socket != null) try { socket.close(); } catch (IOException logOrIgnore) {}
}
}
}
To avoid SO being overloaded by everyone trying this snippet, here's how the output will look like:
HTTP/1.1 200 OK Cache-Control: private Content-Type: text/html; charset=utf-8 Expires: Sun, 21 Feb 2010 20:39:08 GMT Server: Microsoft-IIS/7.5 Date: Sun, 21 Feb 2010 20:39:07 GMT Connection: close Content-Length: 208969
To learn more about sending HTTP requests the low-level way, read the HTTP specification.
However, you probably want to make use of getHeaderFields()
method instead to retrieve a header with multiple values. The getHeaderField()
namely only returns the last value, as per the linked API doc.
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
這篇關于獲取原始 HTTP 響應標頭的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!