Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从asp.net返回一个字节数组并从C#应用程序下载_C#_Asp.net Mvc 4 - Fatal编程技术网

从asp.net返回一个字节数组并从C#应用程序下载

从asp.net返回一个字节数组并从C#应用程序下载,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,所以我有一个asp.net应用程序,它在GET请求中写入一个字节数组,我试图在C#应用程序中接收该字节数组。但我似乎只有在尝试下载时才收到标题。(totalBuff) 我不擅长解释,但我希望代码能让它更清楚 ASP.NET代码: var resp = new HttpResponseMessage(HttpStatusCode.OK); resp.Content = new StreamContent(new FileStream("D:\\Temp\\uLockStub.dll", FileM

所以我有一个asp.net应用程序,它在GET请求中写入一个字节数组,我试图在C#应用程序中接收该字节数组。但我似乎只有在尝试下载时才收到标题。(totalBuff)

我不擅长解释,但我希望代码能让它更清楚

ASP.NET代码:

var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StreamContent(new FileStream("D:\\Temp\\uLockStub.dll", FileMode.Open));
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return resp;
C#代码:

var-httpReq=(HttpWebRequest)
WebRequest.Create(string.Format(“http://localhost:48976/login/login?user={0}&password={1}&hwid={2}“,
用户名,Helper.GetMd5Hash(密码),uid);
var resp=httpReq.GetResponse();
var data=resp.GetResponseStream();
var buff=新字节[1024];
var totalBuff=新列表();
var read=0;
而((read=data.read(buff,0,buff.Length))>0)
{
var tmpBuff=新字节[读取];
块复制(buff,0,tmpBuff,0,read);
totalBuff.AddRange(tmpBuff);
}
我做错了什么

提前谢谢

已解决:

替换

var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StreamContent(new FileStream("D:\\Temp\\uLockStub.dll", FileMode.Open));
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return resp;


解决了。

看看这篇文章,感谢克拉克的回复,但我在ASP.NET方面找到了一个简单的解决方案。将“var resp=new HttpResponseMessage(HttpStatusCode.OK);->return resp;”替换为“Response.BinaryWrite(…)”时,C方收到了所有数据。@user2654057请将您的解决方案作为您问题的答案并接受它,因此,任何稍后在这里遇到问题的人都可以知道解决方案是什么。您应该接受您的答案(单击左侧的V;)@JoeBilly谢谢。这里是新的。:)
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StreamContent(new FileStream("D:\\Temp\\uLockStub.dll", FileMode.Open));
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return resp;
Response.BinaryWrite(System.IO.File.ReadAllBytes("D:\\Temp\\uLockStub.dll"));