问题描述
有没有办法获取原始响应 http 标头?
getHeaderField()
方法对我不起作用,因为服务器吐出多个Set-Cookie",其中一些会丢失.
getHeaderField()
方法对我不起作用
您是在 <代码>java.net.URLConnection,是吗?不,使用 URLconnection
无法获取原始 HTTP 响应标头.您需要退回到低级别的 Socket 编程.这是一个SSCCE,只需复制'n'paste'n'运行它.
为避免每个尝试此代码段的人都使 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 文档.
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.
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.
这篇关于获取原始 HTTP 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!