xamarin postasync Java.IO.EOFException:异常

时间:2019-08-07 11:25:29

标签: c# xamarin.forms

当我尝试将Http帖子发送到具有常规JSON有效负载的后端时,xamarin HTTP客户端出现问题。

这是我的示例代码

    public async Task<T> GenApiSendAsync<T>(Dictionary<string, object> payload, string apiurl)
    {
        try
        {
            var targetUrl = new Uri(HttpApicoreHost(apiurl));

            var json = JsonConvert.SerializeObject(payload);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = UserApicoreAuthToken();
            using (var response = await client.PostAsync(targetUrl, content))
            {
                if (response.IsSuccessStatusCode)
                {
                    var respContent = await response.Content.ReadAsStringAsync();
                    await Commons.DebugAsync(respContent);
                }

                throw new Exception($"[GenApiSendAsync] error targetUrl: {targetUrl} error_msg: {response.ReasonPhrase}");
            }


        }
        catch (EndOfStreamException e)
        {
            Debug.WriteLine($"error EndOfStreamException: {e}");
            throw e;
        }
        catch (Exception e)
        {
            Debug.WriteLine($"[GenApiSendAsync] error {e}");
            throw e;
        }
    }

这是错误消息

        [0:] 
        error Java.IO.EOFException: Exception of type 'Java.IO.EOFException' was thrown.

        at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args)[0x00069] in <23e57773773243b6853e76fc01957c33>:0 

        at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters)[0x0002a] in <23e57773773243b6853e76fc01957c33>:0 

        at Java.IO.InputStream.Read (System.Byte[] b, System.Int32 off, System.Int32 len)[0x00052] in <a2f1be6565a84d5393d8f9efa19f479b>:0 

        at Android.Runtime.InputStreamInvoker.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count)[0x00006] in <a2f1be6565a84d5393d8f9efa19f479b>:0 

        at System.IO.Stream+<>c.<BeginReadInternal>b__40_0 (System.Object<p0>)[0x0000b] in <ff07eae8184a40a08e79049bbcb31a0e>:0 

        at System.Threading.Tasks.Task`1[TResult].InnerInvoke ()[0x0002b] in <ff07eae8184a40a08e79049bbcb31a0e>:0 

        at System.Threading.Tasks.Task.Execute ()[0x00000] in <ff07eae8184a40a08e79049bbcb31a0e>:0 
        --- End of stack trace from previous location where exception was thrown ---


        at System.IO.Stream.EndRead (System.IAsyncResult asyncResult)[0x0004d] in <ff07eae8184a40a08e79049bbcb31a0e>:0 

        at System.IO.Stream+<>c.<BeginEndReadAsync>b__45_1 (System.IO.Stream stream, System.IAsyncResult asyncResult)[0x00000] in <ff07eae8184a40a08e79049bbcb31a0e>:0 

        at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1[TResult, TInstance].Complete (TInstance thisRef, System.Func`3[T1, T2, TResult] endMethod, System.IAsyncResult asyncResult, System.Boolean requiresSynchronization) [0x00000] in <ff07eae8184a40a08e79049bbcb31a0e>:0 
        --- End of stack trace from previous location where exception was thrown ---

          at System.Threading.Tasks.ValueTask`1[TResult].get_Result ()[0x0001b] in <ff07eae8184a40a08e79049bbcb31a0e>:0 
          at System.IO.Stream.CopyToAsyncInternal (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken)[0x000a6] in <ff07eae8184a40a08e79049bbcb31a0e>:0 
          at System.IO.BufferedStream.CopyToAsyncCore (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken)[0x0022e] in <ff07eae8184a40a08e79049bbcb31a0e>:0 
          at System.Net.Http.HttpContent.LoadIntoBufferAsync (System.Int64 maxBufferSize)[0x000a9] in <bded3dbf44d946c7ba5980c587dca714>:0 
          at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken)[0x00191] in <bded3dbf44d946c7ba5980c587dca714>:0 
          at App1Proto.Services.HttpSvc.GenApiSendAsync[T] (System.Collections.Generic.Dictionary`2[TKey, TValue] payload, System.String apiurl) [0x001aa] in D:\Developer\xamarin\App1Proto\App1Proto\Services\HttpSvc.cs:165 
          --- End of managed Java.IO.EOFException stack trace ---
        java.io.EOFException
            at com.android.okhttp.okio.RealBufferedSource.require(RealBufferedSource.java:64)

            at com.android.okhttp.okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.java:270)

            at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.readChunkSize(Http1xStream.java:454)
            at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.read(Http1xStream.java:435)
            at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)

API使用curl可以工作,但是即使我使用curl,我仍然收到错误

curl: (92) HTTP/2 stream 0 was not closed cleanly: INTERNAL_ERROR (err 2)

但我没有使用浏览器。

此外,该API在本地工作,这意味着如果我从设备/仿真器访问我的本地主机Web API。但是如果在暂存环境中,API服务器在Nginx后面运行Dotnet核心,则它将无法正常工作。

有什么线索或建议吗?预先谢谢你。

0 个答案:

没有答案
相关问题