502 when redirecting a stream from another site

In my WEB Api 2 controller, I want to request a file from one site and return this file from my controller. Here is the code

public HttpResponseMessage GetLecture() { HttpWebRequest request = WebRequest.CreateHttp("http://openmedia.yale.edu/cgi-bin/open_yale/media_downloader.cgi?file=/courses/spring11/phil181/mp3/phil181_01_011111.mp3"); request.Referer = @"http://oyc.yale.edu/courses/"; var receivedResponse = request.GetResponse(); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(receivedResponse.GetResponseStream()); response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(receivedResponse.ContentType); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = "phil181_01_011111.mp3"; response.Content.Headers.ContentLength = receivedResponse.ContentLength; return response; } 

Locally, it works fine, and I can upload the file, but when I deploy it to Azure, I get 502 Error. The web server received an invalid response, acting as a gateway or proxy server.

enter image description here

Logging shows that it does not work after the response, so there are no exceptions when executing the method.

This is a ~ 50 MB file. For small files, the code works fine.

How can I get this code to run on Azure for 50 MB files?

+5
source share
2 answers

First of all, I recommend setting up IIS remote administration for your Azure Web App, as this will allow you to directly access IIS logs and related settings. I have a strong feeling that the problem is related to IIS / ASP.NET limitations. You have already indicated that the method completes execution in a few seconds, so this is most likely not a timeout problem.

You also noted that the code works for smaller file sizes, but does not work when the size increases. This indicates an IIS bufferingLimit for your website set to a small value (or perhaps the default value is ~ 4 MB). Here is a link describing how you can increase this limit to the desired value. Hope this works for you. If this is not the case, I would recommend delving into these IIS logs for your site.

+4
source

The error message says about the content, have you tried other types of content, and not the one you got receivedResponse.ContentType here?

In previous comments, you said that the error appears with a Google drive and a higher URL, so it depends on the returned content type.

In addition, you do not return the stream of the entire file as a whole, specifying ContentDisposition as "attachment" .

therefore, perhaps "audio / mpeg" will solve it or some other content type that works with streams.

0
source

Source: https://habr.com/ru/post/1273572/


All Articles