2016-04-26 6 views
0

私はJSON形式でのHttpClientを使用してサーバーに画像や他のフィールドを送信しています。 「Excepionalエラー」:私は私のフロントカメラを持っている小さな画像を送信する場合、私はそれが動作しないと私は常に例外を取得リアカメラを持っている大きな画像を送信する場合 それは、正常に動作しています。Xamarin.Formsの問題は、私のXamarin.Formsアプリから

私は、Windowsフォームアプリケーションで同じコードを作成しようと、それは大きな画像ではなく、私のアプリからも正常に動作しています。

<jsonSerialization maxJsonLength="50000000"/> 

誰かが助けることができます:

は、私はすでにJSONコンテンツのサイズを大きくするために、サーバーのweb.configファイルに変更しました?!ありがとう!!

これが私のアプリのコードです:スタックトレースから

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; 
    } 
+0

完全なスタックトレースを投稿できますか? – SushiHangover

+0

これはどのアプリで動作していませんか? iOSまたはAndroid? –

+0

私はXamarin.FormsとPCL内部のコードを使用しています。この場合、私はAndroid上でアプリケーションを実行しています。 – Stefano

答えて

0

System.Net.WebExceptionStatus.ProtocolError

サーバは、いくつかの40倍に応答したことを意味しますエラー、おそらく401(拒否されたアクセス)またはそれに類するもの。

あなたはあなたの呼び出しをラップすることができます:

try 
{ 
} 
catch(WebException ex){ 
} 

その後、例外にステータスをチェックして、そのような例外の応答をキャスト:間違っているかについての詳細情報を取得するために

((HttpWebResponse)e.Response).StatusDescription 

サーバー上で

EDIT: あなたは大きなファイルをアップロードしているので、あなたが「最大要求の長さが例外を超えて」解決することができ、あなたが大規模な要求を受け入れるようにサーバーの(ASP.NETなど)のweb.configファイルを変更する必要がありますサイズ:

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

TryCatchから、私はこのメッセージを受け取りました: ExceptionMaximum要求長を超えました。、System.Web.HttpRequest.get_InputStream()、ApplicationPostPhotoFromUser(HttpRequestMessage httRequestMessage)D:\ xxx \ ApplicationController.vb:行267にあるSystem.Web.HttpRequest.GetEntireRawContent()で この行には次のコードがあります。私は要求の内容を読む: Dim MyStreamReader As System.IO.StreamReader =新しいSystem.IO.StreamReader(HttpContext.Current.Request.InputStream) – Stefano

+0

更新された答えを確認してください、サーバーの設定で最大要求サイズを増やす必要があります。 –

+0

私はweb.configにこの行を追加してIISを再起動しましたが、今度は500(内部サーバーエラー)を受け取ります – Stefano

関連する問題