I want to make a program that would convert the link (getting a direct link), I will describe the algorithm:

  1. The user inserts the following link into the textbox http://example.com/download/тутданные/file-name.jpg.html/ .

  2. The program of this link takes a fragment (here), assigns it to some variable.

  3. Sends a post request to the host http://example.com/download/getlink with parameters sekret=тутданные,action=getlink_file,downloaded=1

  4. Gets the answer to the following http://какой-то-сайт/api/web/getInstaller?transaction_id=79610604&token=b48aa6bae41a37394677dfe57d3f1a7a&return_url=http%3A%2F%2Fs1.test.ru%2Fu%2Fcdaba1f3138e2e72a3a119b391c52aa4%2F67c00066b6%2FYKuRUX22rvI.jpg )

  5. From this link we take only this part of http%3A%2F%2Fs1.test.%2Fu%2Fcdaba1f3138e2e72a3a119b391c52aa4%2F67c00066b6%2FYKuRUX22rvI.jpg

  6. Open the received link open in the browser.

How to implement it? If you'll post examples, please comment each line.

  • 2
    Not take off. And if the browser code sets a cookie? And if he checks IP? And if the generated link is valid only for 30 seconds? - VladD
  • one
    One gets the impression that this is homework ... Author, have you ever tried? Or are you interested only in the finished result, and even with comments on each line? )) - buratino
  • Cookies are not involved here at all, ip is also not checked, the generated link is available for more than an hour, everything is done very primitively. I did a similar program, only there was an easier way to get a link, and with post requests it’s not how it works. - edvardpotter
  • Real link to any file possible? So at times it would be easier. - Sergey Rufanov 1:16 pm

1 answer 1

The answer should be simple in theory:

 var uri = "http://example.com/download/тутданные/file-name.jpg.html/"; var req = (HttpWebRequest)WebRequest.Create(uri); req.Method = "POST"; req.AllowAutoRedirect = false; string location; using (var resp = (HttpWebResponse)req.GetResponse()) { if (resp.StatusCode != HttpStatusCode.Redirect) { // редирект не пришёл, упс throw new AppropriateException(); } location = resp.GetResponseHeader("Location"); } 

return_url also easy:

 var query = new Uri(location).Query; var queryDict = HttpUtility.ParseQueryString(query); var returnUrl = queryDict["return_url"]; 

But this is all very fragile code. He hopes that the redirect comes in the "right" way (and not, say, through javascript), that the location will be correct, and that there will be a return_url section in it.

(Well, this code almost does not catch errors and performs everything synchronously, which is also wrong.)