我有一个 xamarin 表单应用程序,该应用程序执行PostAsync
调用将图像作为多部分内容上传。
问题是在 iOS 设备中,当我在 PostAsync 调用期间关闭 wifi/移动数据时,HttpClient 不会进入 catch 块来指示异常。它仅在默认的 100 秒超时后才会执行此操作。
但是Android中不会出现这个问题,并且成功进入catch块,表明System.IO.IOException
这是我的代码,它执行PostAsync
打电话,我错过了什么或者做错了什么吗?谢谢。
try{
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
var uri = new Uri(string.Format(strUrl + "UserData/SavePointRequestData/", string.Empty));
StringContent strPointsRequestIDContent = new StringContent(oPointRequest.PointsRequestID.ToString());
multipartContent.Add(strPointsRequestIDContent, "strPointsRequestID");
if (bImageData != null) {
var PointRequestfileContent = new ByteArrayContent(bImageData);
PointRequestfileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
PointRequestfileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
FileName = strActualImageName + ".jpg"
};
multipartContent.Add(PointRequestfileContent, "strUploadImage");
}
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(uri, multipartContent);
if (response.IsSuccessStatusCode) {
using(var stream = await response.Content.ReadAsStreamAsync()) {
using(var streamreader = new StreamReader(stream)) {
var str = await streamreader.ReadToEndAsync();
var obj = await JsonConvert.DeserializeObjectAsync < string > (str);
return obj;
}
}
} else {
return "Error1" + "," + oPointRequest.EventActivityName.ToString();
}
}
catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(e.Message);
return "Error2" + "," + oPointRequest.EventActivityName.ToString();
}
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)