简体   繁体   中英

C# Webclient Download from ASP.NET Controller

I'm having a bit of fun with ASP.NET, WinForms and the WebClient. So I'm using my ASP.NET application as my back end communications for my desktop application and I have had success returning simple string/integer values from the controllers after sending data to them but when I tried to get complex and return entire objects as files, the content returns blank (but it does show the custom content type in the response). Here is my code below, am I doing something wrong? (First time messing with the webclient like this :))

Controller

[AllowAnonymous]
[HttpGet]
public APIRegister(....)
{
   // ..... Create User Account / etc .....
   return File(Methods.Conversions.ObjectToArray(SomeData), "application/custom");
}

The "ObjectToArray" method simply converts the object to a byte array.

Client

HttpWebRequest WR = (HttpWebRequest)HttpWebRequest.Create(MyUrl/Account/APIRegister?Params...);
byte[] RawResponse = new byte[0];
using (HttpWebResponse WD = (HttpWebResponse)WR.GetResponse())
{
    Stream SR = WD.GetResponseStream();
    RawResponse = Methods.Conversions.StreamToArray(SR);
}

The content always returns a length of zero but it does keep the content header type of "application/custom" so it is returning that just not the actual content itself. Any help is appreciated!

Per Federico的评论认为,与使用HTTP GET文件方法相比,尝试将“小数据”分离为JSON请求更好/更可行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM