我目前正在使用Windows手机应用程序点击图片,我想使用HTTP post请求将该图像上传到Web服务。我不想使用windows phone silverlight。
如何将该图片发送到网络服务网址?
答案 0 :(得分:3)
在http上发布图片就像发布任何其他文件类型一样。使用以下代码段
public string PostFileUsingApi()
{
string result = "";
string param1 = "value1";
using (var handler = new HttpClientHandler()) {
using (var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost:8008") }) {
client.Timeout = new TimeSpan(0, 20, 0);
StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
Stream stream = await storageFile.OpenStreamForReadAsync();
var requestContent = new MultipartFormDataContent();
StreamContent fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = "imagekey", //content key goes here
FileName = "myimage"
};
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/bmp");
requestContent.Add(fileContent);
client.DefaultRequestHeaders.Add("ClientSecretKey", "ClientSecretValue");
HttpResponseMessage response = await client.PostAsync("api/controller/UploadData?param1=" + HttpUtility.UrlEncode(param1), requestContent).Result;
if (response.StatusCode == System.Net.HttpStatusCode.OK) {
result = await response.Content.ReadAsStringAsync().Result
} else {
result = "";
}
}
}
return result;
}
安装此软件包以解决依赖关系 https://www.nuget.org/packages/microsoft.aspnet.webapi.client/