nsIChannel
最后修改于Gecko 1.3 To create an HTTP channel, use nsIIOService
with a HTTP URI, for example:
var ios = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); var ch = ios.newChannel("https://www.example.com/", null, null);
方法概述
ACString getRequestHeader(in ACString aHeader); |
ACString getResponseHeader(in ACString header); |
boolean isNoCacheResponse(); |
boolean isNoStoreResponse(); |
void setRequestHeader(in ACString aHeader, in ACString aValue, in boolean aMerge); |
void setResponseHeader(in ACString header, in ACString value, in boolean merge); |
void visitRequestHeaders(in nsIHttpHeaderVisitor aVisitor); |
void visitResponseHeaders(in nsIHttpHeaderVisitor aVisitor); |
属性
属性名 | 类型 | 描述 |
allowPipelining |
boolean |
This attribute is a hint to the channel to indicate whether or not the underlying HTTP transaction should be allowed to be pipelined with other transactions. This should be set to This attribute is 可能抛出的异常
|
redirectionLimit |
unsigned long |
This attribute specifies the number of redirects this channel is allowed to make. If zero, the channel will fail to redirect and will generate a Note: An HTTP redirect results in a new channel being created. If the new channel supports nsIHttpChannel , then it will be assigned a value to its redirectionLimit attribute one less than the value of the redirected channel's redirectionLimit attribute. The initial value for this attribute may be a configurable preference (depending on the implementation). |
referrer |
|
Get or set the URI of the HTTP This attribute may only be set before the channel is opened. Note: The channel may silently refuse to set the Referer: header if the URI does not pass certain security checks (e.g., a "https://" URL will never be sent as the
referrer for a plaintext HTTP request). The implementation is not required to throw an exception when the referrer URI is rejected.可能抛出的异常
|
requestMethod |
ACString |
获取或设置HTTP请求方法(默认为"GET").设置时不区分大小写,获取时返回的都是大写字母组成的字符串. 该属性的值只能在通道打开之前进行设置. Note: The data for a "POST" or "PUT" request can be configured via
nsIUploadChannel . However, after setting the upload data, it may be necessary to set the request method explicitly. The documentation for nsIUploadChannel has further details. 可能抛出的异常
|
requestSucceeded |
boolean |
Returns 可能抛出的异常
|
responseStatus |
unsigned long |
获取HTTP响应状态码(比如200). 只读.
可能抛出的异常
|
responseStatusText |
ACString |
获取HTTP响应状态信息(比如"OK"). Note: This returns the raw (possibly 8-bit) text from the server. There are no assumptions made about the charset of the returned text. You have been warned! Read only.
可能抛出的异常
|
方法
getRequestHeader()
Get the value of a particular request header.
ACString getRequestHeader( in ACString aHeader );
参数
-
aHeader
- 需要查询的请求头名称,不区分大小写(比如"Cache-Control").
返回值
指定请求头的值.
可能抛出的异常
-
NS_ERROR_NOT_AVAILABLE
- 如果没有这个请求头
getResponseHeader()
获取指定响应头的值.
ACString getResponseHeader( in ACString header );
参数
-
header
- 需要查询的响应头名称,不区分大小写(比如"Set-Cookie").
返回值
指定响应头的值.
可能抛出的异常
-
NS_ERROR_NOT_AVAILABLE
-
在响应头还未完全返回的时候调用了该方法(before
onStartRequest()
),或者响应中没有包含此响应头的情况下.
isNoCacheResponse()
Returns true
if the server sent the equivalent of a "Cache-Control: no-cache" response header. Equivalent response headers include: "Pragma: no-cache", "Expires: 0", and "Expires" with a date value in the past relative to the value of the "Date" header.
boolean isNoCacheResponse();
参数
无
返回值
如果服务器返回了"Cache-control: no-cache"或者其他能够禁止缓存的响应头,则该方法返回true
,否则返回false
.
可能抛出的异常
-
NS_ERROR_NOT_AVAILABLE
-
在响应头还未完全返回的时候调用了该方法(before
onStartRequest()
).
isNoStoreResponse()
boolean isNoStoreResponse();
参数
无
返回值
如果服务器返回了"Cache-Control: no-store"这样的响应头,则该方法返回true
,否则返回false
.
可能抛出的异常
-
NS_ERROR_NOT_AVAILABLE
-
在响应头还未完全返回的时候调用了该方法(before
onStartRequest()
).
setRequestHeader()
This method is called to set the value of a particular request header. This method allows, for example, the cookies module to add "Cookie" headers to the outgoing HTTP request. This method may only be called before the channel is opened. If aValue is empty and aMerge is false
, the header will be cleared.
void setRequestHeader( in ACString aHeader, in ACString aValue, in boolean aMerge );
参数
-
aHeader
- 指定请求头的名称,不区分大小写(例如"Cookie").
-
aValue
- 指定请求头的值(例如"X=1").
-
aMerge
-
如果该参数为
true
,则新指定的请求头的值会合并到该请求头已有的值的后面.如果指定的请求头不支持(或者说不适合)新旧值的合并操作,则这个参数会被忽略(比如"Connection"头就只能有一个值).具体那些请求头会忽略掉这个参数,本文档不会给出.如果该参数的值为false
,则新指定的请求头的值会覆盖掉该请求头已有的值.
可能抛出的异常
-
NS_ERROR_IN_PROGRESS
- 在通道已经打开之后才调用了该方法
setResponseHeader()
设置指定响应头的值.This method allows, for example, the HTML content sink to inform the HTTP channel about HTTP-EQUIV headers found in HTML <META> tags. If value is empty and merge is false
, the header will be cleared.
void setResponseHeader( in ACString header, in ACString value, in boolean merge );
参数
-
header
- 指定响应头的名称,不区分大小写(例如"Cache-Control").
-
value
- 指定响应头的值(例如"no-cache").
-
merge
-
如果该参数为
true
,则新指定的响应头的值会合并到该响应头已有的值的后面.如果指定的响应头不支持(或者说不适合)新旧值的合并操作,则这个参数会被忽略(比如"Content-Type"头就只能有一个值).具体那些响应头会忽略掉这个参数,本文档不会给出.如果该参数的值为false
,则新指定的响应头的值会覆盖掉该响应头已有的值.
可能抛出的异常
-
NS_ERROR_NOT_AVAILABLE
-
在响应头还未完全返回的时候调用了该方法(before
onStartRequest()
). -
NS_ERROR_ILLEGAL_VALUE
- If changing the value of this response header is not allowed.
visitRequestHeaders()
Call this method to visit all request headers. Calling setRequestHeader()
while visiting request headers has undefined behavior. Don't do it!
void visitRequestHeaders( in nsIHttpHeaderVisitor aVisitor );
参数
-
aVisitor
- The header visitor instance.
visitResponseHeaders()
Call this method to visit all response headers.
setResponseHeader()
while visiting response headers has undefined behavior. Don't do it! void visitResponseHeaders( in nsIHttpHeaderVisitor aVisitor );
参数
-
aVisitor
- The header visitor instance.
可能抛出的异常
-
NS_ERROR_NOT_AVAILABLE
-
If called before the response has been received (before
onStartRequest()
).