在Xamarin表单中面对HTTPClient post方法中的问题

时间:2018-01-10 10:12:20

标签: xamarin xamarin.forms httpclient

我正面临着http呼叫的问题。

错误消息是:httpclient postasync an error occurred while sending the request

 public TResult PostAPI<T>(string url, T reqparams, List<KeyValuePair<string, string>> formParameters = null, string jsonString = "", List<KeyValuePair<string, string>> headers = null)
    {
        HttpContent contentPost = null;
        if (headers != null)
        {
            AddHeaders(headers);
        }
        if (formParameters != null)
        {
            var formContent = new FormUrlEncodedContent(formParameters);
            contentPost = formContent;

        }
        else if (!string.IsNullOrEmpty(jsonString))
        {
            var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
            contentPost = content;
        }

        string s = JsonConvert.SerializeObject(reqparams);
        var contents = new StringContent(s, Encoding.UTF8, "application/json");
        contentPost = contents;

        //client. = 3000;
        var response = client.PostAsync(url, contentPost).Result; //Task.Run(() => client.PostAsync(url, contentPost)).Result;

        IEnumerable<string> responseHeaders;
        response.Headers.TryGetValues("Authorization", out responseHeaders);

        if (responseHeaders != null && responseHeaders.Any())
        {
            AccessToken = responseHeaders.Select(x => x).FirstOrDefault();
        }

        var apiResult = response.Content.ReadAsStringAsync().Result;
        var result = JsonConvert.DeserializeObject<TResult>(apiResult);
        return result;
    }

抛出此错误:

  

at System.Net.Http.HttpClientHandler + d__63.MoveNext()[0x004ab] in&lt; 7736395a6c71409691a34accfc621095&gt ;:0   ---从抛出异常的先前位置开始的堆栈跟踪结束---     在&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt;中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]:0     在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x0003e]在&LT; 896ad1d315ca4ba7b117efb8dacaedcf&GT;:0在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务) [0x00028]在&LT; 896ad1d315ca4ba7b117efb8dacaedcf&GT;:0在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x00008]在&LT; 896ad1d315ca4ba7b117efb8dacaedcf&GT;:0在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1 + ConfiguredTaskAwaiter [TResult] .GetResult()[0x00000] in&lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt ;:0     在System.Net.Http.HttpClient + d__49.MoveNext()[0x000ca] in&lt; 7736395a6c71409691a34accfc621095&gt ;:0

0 个答案:

没有答案