我有一个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();
}