我将命名空间 System.Net.Http 命名空间与 Xamarin 一起使用,以便调用我的 HTTP REST API,并且它工作正常。我的 http 客户端代码驻留在通用的可移植项目中,我从我的 android-only 项目中调用它(iOS 仍在积压中)
下面是它的样子:
Uri apiUri = new Uri("/example");
var request = (HttpWebRequest)HttpWebRequest.Create(apiUri);
request.ContentType = "application/json";
request.Method = "GET";
WebResponse response = await request.GetResponseAsync();
但是,我怀疑对同一 URL 的连续 GET 被缓存。我已经看到可以通过 HttpRequestCachePolicy 对象删除缓存,如https://developer.xamarin.com/api/type/System.Net.Cache.HttpRequestCachePolicy/ 中所述。我的可能应该看起来像:
Uri apiUri = new Uri("/example");
var request = (HttpWebRequest)HttpWebRequest.Create(apiUri);
request.ContentType = "application/json";
request.Method = "GET";
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
WebResponse response = await request.GetResponseAsync();
但是,命名空间 System.Net.Cache 未在任何地方定义,我无法使用此方法。我正在使用 .NET SDK 4.5。
有没有我遗漏的设置?或者我应该寻找一种不同的方式来禁用 GET 请求缓存?
以下是我得到的错误:
The type or namespace name 'Cache' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)
不管命中 HTTP API 的最佳方法是什么(WebClient 与 HttpClient 与 HttpWebRequest),@ShushiHangover 的评论帮助我们弄清楚我的问题的重点。
PCL 输出格式曾经是 Xamarin“便携式”项目的标准。但是,命名空间System.Net.Cache
在 PCL 库中不可用。它在.NET Standard 2.0 中可用,它本身在 PCL 中不可用。PCL 已于 2017 年夏季弃用,取而代之的是 .NET 标准库,允许访问 .NET Standard 2.0(以及其他好处)。
然而,没有办法将项目从 PCL“转换”到 .NET 标准库,只能创建一个新项目并移动所有文件。因此,我决定创建一个新的 .NET 标准库项目,其中仅包含我的 HTTP 客户端代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句