是否有任何REST库,在那里与便携式类库的工作? [关闭](Are there any REST

2019-07-04 02:47发布

我正在开发一种便携式类库,它需要做出REST请求,并正在寻找类似Restsharp或EasyHttp。 不幸的是这些都不目前与PCLS工作。 这也将是不错的要么看到,确实有基本身份验证的POST请求的例子。

如果没有什么出没有任何人有我会怎么做基本身份验证POST请求的例子吗?

Answer 1:

如果你的目标4.5或Windows Store应用程序,您可以使用HttpClient的为PCL。 否则,你可以尝试破解,并在削减RestSharp的PCL港https://github.com/Geodan/geoserver-csharp/tree/master/RestSharp



Answer 2:

有一个最近刚刚宣布,目前可在GitHub上。 这就是所谓的便携式休息

https://github.com/advancedrei/PortableRest

PortableRest是一种便携式类库用于其它便携式类库实现REST API客户端。 它利用JSON.NET快速,可定制的系列化,还有Microsoft.Bcl.Async库在任何平台上awaitable执行。 它被设计成大幅下降,在兼容RestSharp,但你需要做一些修改和重新编译。

最初的版本中有简单的JSON请求有限的支持。 更多选项(包括XML,并希望DataContract支持)将在未来版本中推出。



Answer 3:

由于我是唯一支持的Windows Phone 7.5或更高,我能够使用这个库( Microsoft.Bcl.Async )以异步suppport添加到我的PLC,然后使用该解决方案 。

所以,我的代码最终看上去是这样的:

 public async Task<RequestResult> RunRequestAsync(string requestUrl, string requestMethod, object body = null)
    {
        HttpWebRequest req = WebRequest.Create(requestUrl) as HttpWebRequest;
        req.ContentType = "application/json";

        req.Credentials = new System.Net.NetworkCredential(User, Password);

        string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", User, Password)));
        var authHeader = string.Format("Basic {0}", auth);
        req.Headers["Authorization"] = authHeader;

        req.Method = requestMethod; //GET POST PUT DELETE
        req.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml";

        if (body != null)
        {
            var json = JsonConvert.SerializeObject(body, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
            byte[] formData = UTF8Encoding.UTF8.GetBytes(json);

            var requestStream = Task.Factory.FromAsync(
                req.BeginGetRequestStream,
                asyncResult => req.EndGetRequestStream(asyncResult),
                (object)null);

            var dataStream = await requestStream.ContinueWith(t => t.Result.WriteAsync(formData, 0, formData.Length));
            Task.WaitAll(dataStream);
        }

        Task<WebResponse> task = Task.Factory.FromAsync(
        req.BeginGetResponse,
        asyncResult => req.EndGetResponse(asyncResult),
        (object)null);

        return await task.ContinueWith(t =>
        {
            var httpWebResponse = t.Result as HttpWebResponse;

            return new RequestResult
            {
                Content = ReadStreamFromResponse(httpWebResponse),
                HttpStatusCode = httpWebResponse.StatusCode
            };

        });
    }

    private static string ReadStreamFromResponse(WebResponse response)
    {
        using (Stream responseStream = response.GetResponseStream())
        using (StreamReader sr = new StreamReader(responseStream))
        {
            //Need to return this response 
            string strContent = sr.ReadToEnd();
            return strContent;
        }
    }

然后,我会打电话的代码是这样的:

public async Task<bool> SampleRequest()
        {            
            var res = RunRequestAsync("https//whatever.com/update/1", "PUT");
            return await res.ContinueWith(x => x.Result.HttpStatusCode == HttpStatusCode.OK);
        }

如果这还不够代码为您随时检查出该项目的其余部分在这里



Answer 4:

你有一个便携式RestSharp在工作:

https://github.com/Geodan/geoserver-csharp/tree/master/RestSharp

看来它的工作好...它使用Json.net到



文章来源: Are there any REST libraries out there that work with Portable Class Libraries? [closed]