【发布时间】:2014-04-15 06:48:02
【问题描述】:
我有三个应用程序。
第一:IIS
二:服务(ASP.NET MVC)
第三:客户端(Winform)
文件存储在 IIS 上。
Service public 一个 api 来根据 URL 将文件下载为字节数组。客户端调用Service的api并按扩展名存储文件。
客户端调用服务后,我检查服务,它返回 15500 字节。但是我发现了客户端,它是 13 个字节。
下面是Service上的代码:
[HttpGet]
public byte[] DownloadData(string serverUrlAddress, string path)
{
if (string.IsNullOrWhiteSpace(serverUrlAddress) || string.IsNullOrWhiteSpace(path))
return null;
// Create a new WebClient instance
using (WebClient client = new WebClient())
{
// Concatenate the domain with the Web resource filename.
string url = string.Concat(serverUrlAddress, "/", path);
if (url.StartsWith("http://") == false)
url = "http://" + url;
byte[] data = client.DownloadData(url);
return data;
}
}
下面是客户端的代码:
static void Main(string[] args)
{
byte[] data = GetData();
File.WriteAllBytes(@"E:\a.pdf", data);
}
public static byte[] GetData()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:54220/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("API/File/DownloadData?serverUrlAddress=www.x.com&path=Data/Folder/file.pdf").Result;
if (response.IsSuccessStatusCode)
{
var yourcustomobjects = response.Content.ReadAsByteArrayAsync().Result;
return yourcustomobjects;
}
else
{
return null;
}
}
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-web-api webclient dotnet-httpclient