nsIChannel
Last changed in 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);
void getOriginalResponseHeader(in ACString aHeader, in nsIHttpHeaderVisitor aVisitor); |
ACString getRequestHeader(in ACString aHeader); |
ACString getResponseHeader(in ACString header); |
boolean isNoCacheResponse(); |
boolean isNoStoreResponse(); |
void redirectTo(in nsIURI aNewURI); |
void setEmptyRequestHeader(in ACString aHeader); |
void setReferrerWithPolicy(in nsIURI referrer, in unsigned long referrerPolicy); |
void setRequestHeader(in ACString aHeader, in ACString aValue, in boolean aMerge); |
void setResponseHeader(in ACString header, in ACString value, in boolean merge); |
void visitOriginalResponseHeaders(in nsIHttpHeaderVisitor aVisitor); |
void visitRequestHeaders(in nsIHttpHeaderVisitor aVisitor); |
void visitResponseHeaders(in nsIHttpHeaderVisitor aVisitor); |
Constant | Description |
REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE |
Default; indicates not to pass on the referrer when downgrading from https to http |
REFERRER_POLICY_NO_REFERRER |
Indicates no referrer will be sent |
REFERRER_POLICY_ORIGIN |
Only send the origin of the referring URI |
REFERRER_POLICY_ORIGIN_WHEN_XORIGIN |
Same as the default; Only send the origin of the referring URI for cross-origin requests |
REFERRER_POLICY_UNSAFE_URL |
Always send the referrer, even when downgrading from https to http |
Attribute | Type | Description |
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 Exceptions thrown
|
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 |
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 Exceptions thrown
|
requestMethod |
ACString |
Set or get the HTTP request method (default is "GET"). Setter is case insensitive; getter returns an uppercase string. This attribute may only be set before the channel is opened. Note: The data for a "POST" or "PUT" request can be configured via Exceptions thrown
|
requestSucceeded |
boolean |
Returns Exceptions thrown
|
responseStatus |
unsigned long |
Get the HTTP response code (For example 200). Read only.
Exceptions thrown
|
responseStatusText |
ACString |
Get the HTTP response status text (For example "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. Exceptions thrown
|
referrerPolicy Read only |
unsigned long |
The referrer policy in use for this channel, indicated by one of the constants listed above |
Get the value of a particular original response header, that is, in the same form as it came from the network. I.e. empty headers will have an empty string as value and multiple headers will not be merged as opposed to getResponseHeader()
.
void getOriginalResponseHeader( in ACString aHeader, in nsIHttpHeaderVisitor aVisitor );
aHeader
aVisitor
nsIHttpHeaderVisitor
instance allowing to visit all equally named response headers.NS_ERROR_NOT_AVAILABLE
onStartRequest()
) or if no header with that name is set in the response.Get the value of a particular request header.
ACString getRequestHeader( in ACString aHeader );
aHeader
The value of the request header.
NS_ERROR_NOT_AVAILABLE
Get the value of a particular response header.
ACString getResponseHeader( in ACString header );
header
The value of the response header.
NS_ERROR_NOT_AVAILABLE
onStartRequest()
) or if the header is not set in the response.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();
None.
Returns true
if the server sent the equivalent of a "Cache-control: no-cache" response header, otherwise false
.
NS_ERROR_NOT_AVAILABLE
onStartRequest()
).boolean isNoStoreResponse();
None.
true
if the server sent a "Cache-Control: no-store" response header.
NS_ERROR_NOT_AVAILABLE
onStartRequest()
).Instructs the channel to immediately redirect to a new destination. Can only be called on channels not yet opened.
This method provides no explicit conflict resolution. The last caller to call it wins.
void redirectTo( in nsIURI aNewURI );
aNewURI
NS_ERROR_ALREADY_OPENED
This method is called to set an empty value for a particular request header. This should be used with caution in the cases where the behavior of setRequestHeader()
ignoring empty header values is undesirable. This method may only be called before the channel is opened.
void setEmptyRequestHeader( in ACString aHeader );
aHeader
NS_ERROR_IN_PROGRESS
NS_ERROR_FAILURE
visitRequestHeaders()
.Call this method to set the channel's referrer using the referrer policy indicated with one of the predefined constants.
void setReferrerWithPolicy( in nsIURI aReferrer, in unsigned long aReferrerPolicy );
aReferrer
aReferrerPolicy
NS_ERROR_FAILURE
visitRequestHeaders()
.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
aValue
aMerge
true
, the new header value will be merged with any existing values for the specified header. This flag is ignored if the specified header does not support merging (For example the "Content-Type" header can only have one value). The list of headers for which this flag is ignored is an implementation detail. If this flag is false
, then the header value will be replaced with the contents of aValue
.NS_ERROR_IN_PROGRESS
NS_ERROR_FAILURE
visitRequestHeaders()
.Set the value of a particular response header. 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
value
merge
true
, the new header value will be merged with any existing values for the specified header. This flag is ignored if the specified header does not support merging (For example the "Content-Type" header can only have one value). The list of headers for which this flag is ignored is an implementation detail. If this flag is false, then the header value will be replaced with the contents of value
.NS_ERROR_NOT_AVAILABLE
onStartRequest()
).NS_ERROR_ILLEGAL_VALUE
NS_ERROR_FAILURE
visitResponseHeaders()
, visitOriginalResponseHeaders()
or getOriginalResponseHeader()
.Call this method to visit all original response headers, that is, in the same form as they came from the network. I.e. empty headers will be have an empty string as value and multiple headers will not be merged as opposed to visitResponseHeaders()
.
Warning: Calling setResponseHeader()
while visiting response headers will return a NS_ERROR_FAILURE
.
void visitOriginalResponseHeaders( in nsIHttpHeaderVisitor aVisitor );
aVisitor
nsIHttpHeaderVisitor
instance allowing to visit all original response headers.Call this method to visit all request headers.
Warning: Calling setRequestHeader()
, setReferrerWithPolicy()
or setEmptyRequestHeader()
while visiting request headers has undefined behavior until Gecko 47 (Firefox 47.0 / Thunderbird 47.0 / SeaMonkey 2.44). Starting from Gecko 48 (Firefox 48.0 / Thunderbird 48.0 / SeaMonkey 2.45) they will return a NS_ERROR_FAILURE
.
void visitRequestHeaders( in nsIHttpHeaderVisitor aVisitor );
aVisitor
Call this method to visit all response headers.
Warning: Calling setResponseHeader()
while visiting response headers has undefined behavior until Gecko 48 (Firefox 48.0 / Thunderbird 48.0 / SeaMonkey 2.45). Starting from Gecko 49 (Firefox 49.0 / Thunderbird 49.0 / SeaMonkey 2.46) it will return a NS_ERROR_FAILURE
.
void visitResponseHeaders( in nsIHttpHeaderVisitor aVisitor );
aVisitor
NS_ERROR_NOT_AVAILABLE
onStartRequest()
).Note: Starting from Firefox 49, empty headers will be returned in case the preference network.http.keep_empty_response_headers_as_empty_string
is set to true
. Since Firefox 50 the preference defaults to true
.