我目前正在尝试通过我的网络服务上传图片。我有这段代码:
public async Task<Webservice> editProfile(List<KeyValuePair<string, string>> values, byte[] image)
{
String strUrl = String.Format("http://********/nl/webservice/abc123/members/update");
var HttpClientUpload = new HttpClient();
HttpMultipartFormDataContent requestUploadContent = new HttpMultipartFormDataContent();
Stream streamImage = new System.IO.MemoryStream(image);
HttpStreamContent streamContent = new HttpStreamContent(streamImage.AsInputStream());
requestUploadContent.Add(streamContent, "myFile", "image.jpg");
foreach (var keyValuePair in values)
{
requestUploadContent.Add(new HttpStringContent(keyValuePair.Value), keyValuePair.Key);
}
HttpResponseMessage responseLogin = await HttpClientUpload.PostAsync(new Uri(strUrl), requestUploadContent);
responseLogin.EnsureSuccessStatusCode();
if (responseLogin.StatusCode == Windows.Web.Http.HttpStatusCode.Ok && responseLogin.Content != null)
{
var data = new Webservice { Status = responseLogin.StatusCode };
data.Data = await responseLogin.Content.ReadAsStringAsync();
Debug.WriteLine(data.Data);
return data;
}
return new Webservice { Status = new Windows.Web.Http.HttpStatusCode() };
}
但我总是得到以下异常:'System.ExecutionEngineException'
在以下一行:
HttpStreamContent streamContent = new HttpStreamContent(streamImage.AsInputStream());
任何人都可以帮忙,现在几天都在努力解决这个问题......
提前致谢!
修改
我改变了我的功能:
public async Task<Webservice> editProfile(List<KeyValuePair<string, string>> values, byte[] image)
{
try
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = iso.OpenFile("image.jpg", FileMode.Open, FileAccess.Read))
{
String strUrl = String.Format("http://membr.sanmax.be/nl/webservice/abc123/members/update");
HttpMultipartFormDataContent requestUploadContent = new HttpMultipartFormDataContent();
//Stream streamImage = new System.IO.MemoryStream(stream);
HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());
requestUploadContent.Add(streamContent, "picture", "photo.jpg");
foreach (var keyValuePair in values)
{
requestUploadContent.Add(new HttpStringContent(keyValuePair.Value), keyValuePair.Key);
}
HttpResponseMessage responseLogin = await httpClient.PostAsync(new Uri(strUrl), requestUploadContent);
responseLogin.EnsureSuccessStatusCode();
if (responseLogin.StatusCode == Windows.Web.Http.HttpStatusCode.Ok && responseLogin.Content != null)
{
var data = new Webservice { Status = responseLogin.StatusCode };
data.Data = await responseLogin.Content.ReadAsStringAsync();
Debug.WriteLine(data.Data);
return data;
}
}
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
return new Webservice { Status = new Windows.Web.Http.HttpStatusCode() };
}
但是现在它在catch部分停止了这个例外:
WinRT information: Response status code does not indicate success: 413 (Request Entity Too Large).
System.Exception: Request entity too large (413).