5

I am trying to get real path in link megaupload but always but this dont work.

function getRealURL(){

    var st = new String(""); 
    var req = new XMLHttpRequest();
    req.open("GET","http://www.megaupload.com/?d=6CKP1MVJ",true);
    req.send(null);
    req.send(null);
    req.onreadystatechange = function (aEvt) {
     if (req.readyState == 4) {
        if(req.status == 302){
          //SUCESSO
           st = req.responseText;
        }
      }
    };//funcao

    element.getElementById("id").setAttribute("value", st);

}

i need this link:

Redirect to: http://www534.megaupload.com/files/c2c36829bc392692525f5b7b3d9d81dd/Coldplay - Warning Sign.mp3

insted of this:

http://www.megaupload.com/?d=6CKP1MVJ
Zoe
  • 27,060
  • 21
  • 118
  • 148
ericmoraess
  • 374
  • 1
  • 7
  • 16
  • Seems to me that this URL exists (returns 200, not 302). If you need the other link, why don't you use that one directly? Or are you trying to write something generic for megaupload downloads? – Eran Zimmerman Gonen Sep 23 '11 at 05:02

1 Answers1

9

XMLHttpRequest follows the redirect automatically by default so you don't see the 302 response. You need to set nsIHttpChannel.redirectionLimit property to zero to prevent it:

req.open("GET","http://www.megaupload.com/?d=6CKP1MVJ",true);
req.channel.QueryInterface(Components.interfaces.nsIHttpChannel).redirectionLimit = 0;
req.send(null);

Not that the link you use here redirects anywhere but this is the general approach. Btw, instead of looking at the response text for redirects you should look at req.getResponseHeader("Location").

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • This doesn't seem to work anymore. In Chrome (29.0) I get `Uncaught ReferenceError: Components is not defined` and in Firefox (24.0) I get `TypeError: xhr.channel is undefined`. Also, the MDN page on Components says you should no longer use it. https://developer.mozilla.org/en/docs/Components_object – mflodin Sep 30 '13 at 13:29
  • 1
    @mflodin: This is a question about **Firefox extensions**. It isn't very surprising that this code doesn't work in Chrome. Also, it isn't very surprising that low-level API like `XMLHttpRequest.channel` isn't accessible to web pages in Firefox. Only privileged code (meaning an extension) can access it. – Wladimir Palant Oct 01 '13 at 10:14
  • 1
    @WladimirPalant Ah, sorry. I missed that. Had a lot of Stack Overflows questions open on 302 issues. I'll be sure to check the tags more carefully in the future. – mflodin Oct 03 '13 at 07:07