2

I have some old Web API action method which returns CSV file. It worked for long time, but recently stopped. Now it causes ERR_SPDY_PROTOCOL_ERROR.

ERR_SPDY_PROTOCOL_ERROR in Chrome is often associated with Avast security as described here. In my case however it's not caused by Avast, and other web browsers throw exceptions too.

My action method looks as follows:

[HttpGet]
[Route("csv")]
public HttpResponseMessage SomeMethod([FromUri]SomeSearchCriteria sc)
{
    using (MemoryStream stream = new MemoryStream())
    {
        StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
        string content = someLogic.SomeSearchmethod(sc);
        writer.Write(content);
        writer.Flush();
        stream.Position = 0;

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
        return result;
    }              
}

The method is called by angular front end by simple change of window.location on button click.

Whole action method is executed properly, with no exceptions. Error is shown only by web browser.

Flushing sockets in Chrome as described here does not solve the issue.

Community
  • 1
  • 1
Arkadiusz Kałkus
  • 17,101
  • 19
  • 69
  • 108
  • In what browsers (and what are versions of those browsers) you experience this error? – Evk Mar 25 '17 at 13:01
  • This specific error in (as far as I remember newest) Chrome, but in IE, and Firefox there are errors too, however there's no error message. – Arkadiusz Kałkus Mar 25 '17 at 22:21

1 Answers1

6

I have tried this method in API controller and call through chrome browser, it throws net::ERR_CONNECTION_RESET

There is some issue in response filled with StreamContent, use ByteArrayContent in result content, it works perfectly.

    [HttpGet]
    [Route("csv")]
    public HttpResponseMessage SomeMethod([FromUri]SomeSearchCriteria sc)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            string content = "test";
            writer.Write(content);
            writer.Flush();
            stream.Position = 0;

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            //result.Content = new StreamContent(stream);
            result.Content = new ByteArrayContent(stream.ToArray());
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
            return result;
        }
    }
Jignesh Variya
  • 1,869
  • 16
  • 12