How to call REST services from Portable Class Libr

2019-04-28 12:58发布

问题:

My target projects are Windows 8, WinRT and Windows Phone 8. I am using Portable Class Libraries for the solution in order to share the sources. I need to call some REST services created in another MVC Web Api project but HttpClient class is not available in the PCL. Which would be a good approach to solve it? I was thinking in a service interface and then to create a project for each platform, using HttpClient, WebClient or the appropriate class in each case. This approach could work but I was wondering if there are other ways.

回答1:

HttpClient is not necessarily portable from Microsoft's view. The only other way is to create separate libraries that each project will implement, and do as you have described. This way, you can achieve commonality across all the platforms you desire to target.

I thought there was some level of equivalency when it came to the platforms you are targeting... If you have to have separate projects, you could link your code files to the other project so you don't have to maintain multiple projects, or even use Project Linker (though I don't know if a 2012 version exists).



回答2:

Microsoft has rewritten the HttpClient library to be portable (PCL) and it's here on NuGet. For the time being, it's only available as pre-release so if you're using the NuGet GUI package manager make sure you set to "Include Prerelease". From the command line:

Install-Package Microsoft.Net.Http


回答3:

Portable REST has recently become available on GitHub and should help making web requests given that WebClient is unavailable:

https://github.com/advancedrei/PortableRest#readme

PortableRest is a Portable Class Library for implementing REST API clients in other Portable Class Libraries. It leverages JSON.NET for rapid, customizable serialization, as well as the Microsoft.Bcl.Async library for awaitable execution on any platform. It is designed to be largely drop-in compatible with RestSharp, though you will need to make some changes and recompile.

This initial release has limited support for simple JSON requests. More options (including XML and hopefully DataContract support) will be available in later releases.



回答4:

Quite late, but here's a simple code I had to do, PLC is targeted for everything except XBox 360 ( might work too ). I could not use HttpClient for this target list.

public class WebDownload
{
    public class WebDownloadResult
    {
        public HttpStatusCode StatusCode { get; set; }
        public int StatusCodeNumber { get; set; }
        public bool ErrorOccured { get; set; }
        public string ResultString { get; set; }
    }

    public static void Download(string url, Action<WebDownloadResult> resultAction)
    {
        WebDownloadResult response = new WebDownloadResult();
        try
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            IAsyncResult result = (IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult tempResult)
            {
                HttpWebResponse webResponse = (HttpWebResponse)myHttpWebRequest.EndGetResponse(tempResult);
                Stream responseStream = webResponse.GetResponseStream();

                using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
                {
                    response.ResultString = reader.ReadToEnd();
                    response.StatusCode = webResponse.StatusCode;
                    response.StatusCodeNumber = (int)webResponse.StatusCode;

                    if (resultAction != null) resultAction(response);
                }
            }), null);
        }
        catch 
        {
            response.ErrorOccured = true;
            if (resultAction != null) resultAction(response);
        }
    }
}