【问题标题】:Xamarin.Forms problems sending large picture over HttpClient SendAsync in Json formatXamarin.Forms 问题通过 HttpClient SendAsync 以 Json 格式发送大图片
【发布时间】: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


【解决方案1】:

从堆栈跟踪,

System.Net.WebExceptionStatus.ProtocolError

表示服务器响应了一些 40x 错误,可能是 401(拒绝访问)或类似的东西。

你可以把你的电话包装成:

try
{
}
catch(WebException ex){
}

然后检查异常状态并像这样转换异常的响应:

((HttpWebResponse)e.Response).StatusDescription

获取有关服务器出现问题的更多信息。

编辑: 因为你上传的是大文件,你可以解决“最大请求长度超出异常”,你需要修改你的服务器(例如 ASP.NET)的 web.config 文件来接受大的请求大小:

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="50000" />
    </system.web>
</configuration>

【讨论】:

  • 从 TryCatch 中我收到了这条消息:超出了异常最大请求长度。在 System.Web.HttpRequest.GetEntireRawContent() 在 System.Web.HttpRequest.get_InputStream() 在 ApplicationPostPhotoFromUser(HttpRequestMessage httRequestMessage) 在 D:\xxx\ApplicationController.vb:line 267。在这一行中,我在其中阅读了以下代码请求内容: Dim MyStreamReader As System.IO.StreamReader = New System.IO.StreamReader(HttpContext.Current.Request.InputStream)
  • 请检查更新后的答案,您需要增加服务器配置的最大请求大小。
  • 我在 web.config 中添加了这一行并重新启动了 IIS,但现在我得到 500(内部服务器错误)
  • 对不起,我不确定,也许您可​​以调试接受上传图像的服务器端方法并查看确切的异常详细信息。
  • 我已经能够解决 500(内部服务器错误)然后我收到一个错误,字符串的 JSON JavaScriptSerializer 长度超过了 maxJsonLength 属性上设置的值。我在代码上增加了这个值(MyJs.MaxJsonLength = Int32.MaxValue),现在它可以工作了!!非常感谢!!!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多