Windows form and WEB API RRS feed

  • Question

  • Helo,

    I'm developing a windows form application (c#) that calls WEB API services (developed by me) and in particular I'm trying to "downloading" an Excel file (report) that it was previously created in a server folder ("Stampe").

    I'm in trouble to "manage" client side, the response (HttpResponseMessage) from WEB API service.

    • SERVER side:

            public HttpResponseMessage GetFile(string FilePath, string FileName)
                return GetFileStampa(FilePath, FileName);

            public HttpResponseMessage GetFileStampa(string FilePath, string fileName)
                var path = Path.Combine(FilePath, fileName);

                var response = new HttpResponseMessage(HttpStatusCode.OK);

                var stream = new FileStream(path, FileMode.Open);
                response.Content = new StreamContent(stream);

                response.Content.Headers.ContentType =
                  new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition =
                  new ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = fileName;

                return response;

    • CLIENT side (windows form):

    string FilePath = @"........\Stampe\";
                string FileName = @"Test.xlsx";

                string UrlGetFile = Url + "/File?" + "FilePath=" + FilePath + "&FileName=" + FileName;
                Uri UriGetFile = new Uri(UrlGetFile);
                HttpWebRequest request = WebRequest.Create(UriGetFile) as HttpWebRequest;
                request.Method = "GET";
                request.ContentType = "text/xml";
                string results = string.Empty;
                HttpWebResponse response;
                using (response = request.GetResponse() as HttpWebResponse)
                    Stream objStream = response.GetResponseStream();
                    BinaryReader breader = new BinaryReader(objStream);
                    byte[] data = breader.ReadBytes((int)response.ContentLength);
                    File.WriteAllBytes("d:\\temp\\MyTest.xlsx", data);

    the instruction "response.ContentLength" is always to -1. Message error: Non-negative number required

    Where am I doing wrong (client or server side)?



    Tuesday, May 21, 2019 2:42 PM

All replies

  • A WebAPI looks to be a octet-stream response, but your client-side program is expecting XML, which is going to cause the client to blow-up eventually I would think.

    You could discuss what you should be doing at the ASP.NET WebAPI forum.


    Tuesday, May 21, 2019 4:08 PM
  • Hello,

    Thank you for your answer. I will follow your advice (XML and asp.net formu)



    Tuesday, May 21, 2019 4:15 PM

  • Hi

    Thank you for posting here.

    Since this thread is related to asp.net, you could post in the following forum.


    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 23, 2019 9:23 AM