【发布时间】:2016-04-26 21:05:10
【问题描述】:
从我的 Xamarin.Forms 应用程序中,我使用 Json 格式的 HttpClient 将图片和其他字段发送到服务器。 如果我发送一张我用前置摄像头拍摄的小照片,它工作正常,如果我发送一张我用后置摄像头拍摄的大图,它不起作用,我总是得到一个例外:“异常错误”。
我尝试在 Windows 窗体应用程序中创建相同的代码,它也适用于大图像,但不是来自我的应用程序。
我已经在服务器上修改了 web.config 以增加 json 内容大小:
<jsonSerialization maxJsonLength="50000000"/>
有人可以帮忙吗?!谢谢!!
这是我的应用程序上的代码:
public async static Task<MyAppDataModels.Common.WsResponse> PostPhotoFromUser(int catalogItemId, int objReferenceId, int languageId, byte[] fileContent)
{
MyAppDataModels.Common.WsResponse MyResponse = new MyAppDataModels.Common.WsResponse();
try
{
HttpClient MyHttpClient = new HttpClient();
MyHttpClient.Timeout = TimeSpan.FromSeconds(520);
MyHttpClient.BaseAddress = new Uri(string.Format("{0}/Application/ApplicationPostPhotoFromUser", MyAppSettings.ServerApiUrl));
MyHttpClient.DefaultRequestHeaders.Accept.Clear();
MyHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
MyHttpClient.DefaultRequestHeaders.AcceptCharset.Clear();
MyHttpClient.DefaultRequestHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
HttpRequestMessage MyWsRequest = new HttpRequestMessage(HttpMethod.Post, MyHttpClient.BaseAddress);
dynamic MyObjData = new JObject();
MyObjData["CatalogItemId"] = catalogItemId;
MyObjData["ObjReferenceId"] = objReferenceId;
MyObjData["UserId"] = string.Empty;
MyObjData["LanguageId"] = languageId;
MyObjData["Picture"] = fileContent;
string MySerializedPostedData = JsonConvert.SerializeObject(MyObjData);
MyWsRequest.Content = new StringContent(MySerializedPostedData, Encoding.UTF8, "application/json");
HttpResponseMessage MyWsResponse = await MyHttpClient.SendAsync(MyWsRequest, HttpCompletionOption.ResponseHeadersRead);
MyWsResponse.EnsureSuccessStatusCode();
var MyContentResponse = await MyWsResponse.Content.ReadAsStringAsync();
MyResponse.ResponseId = MyAppConstants.Constants.ResponseCode.Successfull;
}
catch (Exception ex)
{
MyResponse.ResponseId = MyAppConstants.Constants.ResponseCode.ErrorWhileProcessingRequest;
MyResponse.ResponseErrorMessage = ex.Message;
}
return MyResponse;
}
【问题讨论】:
-
你能发布完整的堆栈跟踪吗?
-
这在哪个应用程序上不起作用? iOS 还是安卓?
-
我正在使用 Xamarin.Forms 和它在 PCL 中的代码,在这种情况下,我在 Android 上运行应用程序时遇到问题
标签: json xamarin httpclient xamarin.forms sendasync