0

from my Xamarin.Forms app I'm sending a picture and other fields to the server using HttpClient in Json format. If I send a little picture I've got with my front camera it's working fine, if I send a larger picture I've got with the rear camera it doesn't work and I always get an exception: "Excepional error".

I tried to create the same code in a windows form application and it's working fine also with large images, but not from my App.

I have already modified on the server web.config to increase json content size:

<jsonSerialization maxJsonLength="50000000"/>

Can someone help ?! Thanks!!

This is the code on my app:

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;
    }
4

1 回答 1

0

从堆栈跟踪,

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>
于 2016-04-26T22:06:51.667 回答