Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

nsIHttpChannel

This interface allows for the modification of HTTP request parameters and the inspection of the resulting HTTP response status and headers when they become available.
继承于: 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 false, for example, if the application knows that the corresponding document is likely to be very large.

This attribute is true by default, though other factors may prevent pipelining.

This attribute may only be set before the channel is opened.
可能抛出的异常
NS_ERROR_FAILURE
If set after the channel has been opened.
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 NS_ERROR_REDIRECT_LOOP failure status.

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 nsIURI

Get or set the URI of the HTTP Referer: header. This is the address (URI) of the resource from which this channel's URI was obtained (see RFC2616 section 14.36).

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.
可能抛出的异常
NS_ERROR_IN_PROGRESS
If set after the channel has been opened.
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.

可能抛出的异常
NS_ERROR_IN_PROGRESS
If set after the channel has been opened.
requestSucceeded boolean

Returns true if the HTTP response code indicates success. The value of nsIRequest.status() will be NS_OK even when processing a 404 File Not Found response because such a response may include a message body that (in some cases) should be shown to the user. Use this attribute to distinguish server error pages from normal pages, instead of comparing the response status manually against the set of valid response codes, if that is required by your application. Read only.

可能抛出的异常
NS_ERROR_NOT_AVAILABLE
If called before the response has been received (before onStartRequest()).
responseStatus unsigned long 获取HTTP响应状态码(比如200). 只读.
可能抛出的异常
NS_ERROR_NOT_AVAILABLE
If called before the response has been received (before onStartRequest()).
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.
可能抛出的异常
NS_ERROR_NOT_AVAILABLE
If called before the response has been received (before onStartRequest()).

方法

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.

Warning: Calling 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()).

文档标签和贡献者

 此页面的贡献者: ziyunfei
 最后编辑者: ziyunfei,