Using HttpClient for Xamarin PCL

本文关键字:PCL Xamarin for HttpClient Using | 更新日期: 2023-09-27 18:05:31

我正在Xamarin中创建一个PCL,该PCL使用Microsoft.Net.Http Nuget包作为HttpClient类。我使用HttpClient将音频文件发布到web API。到目前为止,这对我来说工作得很好,但现在我在试图将文件流式传输到API时遇到了麻烦。API在其文档中表示,为了接受流媒体文件,需要添加一个"传输编码"头,将其"分块"。我不能手动添加这个头与其他头的方式,而是从HttpClient的属性设置它。问题是,当我调用HttpClient.PostAsync()时,它会抛出一个异常,因为HttpWebRequest的SendChunk属性不是True。我假设HttpWebRequest对象是由HttpClient创建的,但我不知道如何将SendChunk属性设置为True。当我自己创建一个HttpWebRequest对象时,SendChunk属性是不可访问的,即使它是一个公共属性。我猜它与作为PCL有关,但SeundChunk属性确实出现在Microsoft.Net.Http程序集中。

简而言之,我需要得到标题"传输编码:块"到我的HttpClient头,但不能。我如何添加此标题,而某些属性不可用,因为我在PCL工作?

public async Task<string> PostAsync(string url, Stream content, Dictionary<string,string> requestHeaders, Dictionary<string,string> contentHeaders)
    {
        var httpClient = new System.Net.Http.HttpClient();
        var httpContent = new System.Net.Http.StreamContent(content);
        httpClient.Timeout = TimeSpan.FromSeconds(20);
        httpClient.DefaultRequestHeaders.TransferEncodingChunked = true;
        if (requestHeaders != null)
        {
            foreach (var headerName in requestHeaders.Keys)
            {
                httpClient.DefaultRequestHeaders.Add(headerName, requestHeaders[headerName]);
            }
        }
        if (contentHeaders != null)
        {
            foreach (var headerName in contentHeaders.Keys)
            {
                httpContent.Headers.Add(headerName, contentHeaders[headerName]);
            }
        }
        HttpResponseMessage response = null;
        try
        {
            response = await httpClient.PostAsync(url, httpContent);
        }
编辑:

我已经更新了我的代码,看起来像这样

             HttpClientHandler handler = new HttpClientHandler();
        System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(handler);
        httpClient.DefaultRequestHeaders.TransferEncodingChunked = true;
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
        var httpContent = new System.Net.Http.StreamContent(content);
        request.Content = httpContent;
        if (handler.SupportsTransferEncodingChunked())
        {
            request.Headers.TransferEncodingChunked = true;
        }
        HttpResponseMessage response = null;

            response = await httpClient.SendAsync(request);

SendAsync抛出一个异常,说"System.Net. net。"ProtocolViolationException: SendChunked应该是true"。我模仿这篇文章http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx.

Using HttpClient for Xamarin PCL

我正试图处理类似的情况。我遇到了几个可能有用的选项。

ModernHTTPClient

或其他锋利的

作为状态更新,我确实使ModernHTTPClient工作。我的解决方案是删除包文件夹的内容,然后安装nuget包,然后恢复其他包。否则,解决方案就像

一样简单。
var httpClient = new HttpClient(new NativeMessageHandler());

我从Xamarin论坛的这个链接找到了一个简单的库;https://forums.xamarin.com/discussion/25274/httpclient-in-pcl

有一个由Robert McLaws和Richard Beauchamp编写的Nuget Package,叫做portableest。

它是一个从可移植类库(Portable Class Libraries)中使用REST api的库。。NET 4.5, Silverlight 5, Windows Phone 8。x和Windows 8.x)。设计为部分插入式兼容RestSharp。

GitHub链接在这里:https://github.com/AdvancedREI/PortableRestNuget包在这里:https://www.nuget.org/packages/PortableRest

来源:https://forums.xamarin.com/discussion/25274/httpclient-in-pcl

使用:

var client = new RestClient { BaseUrl = Constants.API_BASE_URI };
var request = new RestRequest("Token", HttpMethod.Post, ContentTypes.FormUrlEncoded);
request.AddHeader("Accept", "application/json");
request.AddParameter("grant_type", "password", ParameterEncoding.UriEncoded);
request.AddParameter("username", userEntry.Text, ParameterEncoding.UriEncoded);
request.AddParameter("password", passwordEntry.Text, ParameterEncoding.UriEncoded);
var result = await client.SendAsync<AuthToken>(request);