简体   繁体   English

如何取消 http 请求?

[英]How to cancel http requests?

I'm tring to cancel HTTP request using aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED);我正在尝试使用aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED); but it dont work.但它不起作用。 The code is given below.代码如下。 Can anyone please correct it?任何人都可以纠正它吗?

function(aSubject, aTopic, aData)
{
    if ("http-on-modify-request" == aTopic)
    {
        //get the http request url
            var url = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).originalURI.spec;

        //get the referer of http request (this error gives an error; dont know why :( 
        var a = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).getRequestHeader("referer");

        //Cancel http request from other domains 
        if (url.split('//')[1].split('/')[0] != a.split('//')[1].split('/')[0])
        {
            //the code below don't work! Someone please correct it.
            //code to cancel http request
            aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED);
            }
    }
}

That's probably because NS_BINDING_SUCCEEDED isn't an error code (yes, the documentation seems to be wrong).这可能是因为 NS_BINDING_SUCCEEDED 不是错误代码(是的,文档似乎是错误的)。 What you actually want to use is Components.results.NS_BINDING_ABORTED .您真正想要使用的是Components.results.NS_BINDING_ABORTED

By the way, you seem to be comparing the host name in the URL.顺便说一句,您似乎在比较 URL 中的主机名。 Given that you already have an nsIURI object this can be done easier:鉴于您已经拥有 nsIURI object,这可以更轻松地完成:

var requestHost = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).originalURI.host;
var referrerHost = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).referrer.host;
if (requestHost != referrerHost)
{
    aSubject.cancel(Components.results.NS_BINDING_ABORTED);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM