对于重定向下载链接捕获HTTP状态300 [英] Capture HTTP Status 300 for redirect download links

查看:340
本文介绍了对于重定向下载链接捕获HTTP状态300的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Chrome扩展下载网页上的链接。 我碰到一些环节有没有直接下载,比如像这样的的http:// downloads.sourceforge.net/sevenzip/7z920-x64.msi

I'm developing a Chrome Extension to download links on a webpage. I came across some links there were not a direct download, for example like this one http://downloads.sourceforge.net/sevenzip/7z920-x64.msi

当我跟踪的小提琴手的链接,我得到的 HTTP状态302 (这是一个重定向)。所以我想用XMLHTT prequest下载之前测试链接,但我不能找回状态的 302 ,XMLHTT prequest只检索文件状态是 200

When I trace the link on fiddler, I'm getting HTTP Status 302 (which is a redirect). So I'm trying to test the link before downloading using XMLHTTPRequest, but I can't retrieve status 302, XMLHTTPRequest is only retrieving the file status which is 200?

有没有解决这个一招,使之停止在302或者其他替代AJAX调用?

Is there a trick around this to make it stop at 302 or perhaps a different alternative to AJAX calls?

推荐答案

这是可能与使用的 chrome.webRequest API。 302 的之类,通常由浏览器自动处理,但你可以用这个方法来阻止他们或让你的code更为明显。

This is possible with the use of the chrome.webRequest api. 302's and the like are normally handled silently by the browser, but you can use this method to stop them or make them more visible to your code.

chrome.webRequest.onHeadersReceived.addListener(function(details){
    var redirUrl;
    details.responseHeaders.forEach(function(v,i,a){
      if(v.name == "Location"){
       redirUrl = v.value;
       details.responseHeaders.splice(i,1);  //Kill the redirect
      }
    });
    if(redirUrl)
      details.responseHeaders.push({name:"redirUrl",value:redirUrl});
    return {responseHeaders:details.responseHeaders}; 
},
{urls: ["http://*/*"],tabId:-1},["responseHeaders","blocking"]);

我指定了一个 tabId -1,这意味着它应该只对请求不是来自一个选项卡来了,也就是一个背景页。这将默默地prevent重定向,同时允许您访问这两个 302 状态和URL重定向到。

I specified a tabId of -1, which means it should only work for requests not coming from a tab, i.e. a background page. This will silently prevent the redirect while allowing you access to both the 302 status and the url to redirect to.

这篇关于对于重定向下载链接捕获HTTP状态300的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆