c# – 为什么我的HttpWebRequest类中缺少UserAgent属性?

c# – 为什么我的HttpWebRequest类中缺少UserAgent属性?,第1张

概述当我意识到它丢失时,我试图将该值从PCL设置为一个值.这是我的HttpWebRequest类的样子: #region Assembly System.Net.Requests, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a// C:\Program Files (x86)\Reference Assemblie 当我意识到它丢失时,我试图将该值从PCL设置为一个值.这是我的httpWebRequest类的样子:
#region Assembly System.Net.Requests,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a// C:\Program files (x86)\Reference AssemblIEs\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile111\System.Net.Requests.dll#endregionusing System.IO;namespace System.Net{    //    // Summary:    //     ProvIDes an http-specific implementation of the System.Net.WebRequest class.    public class httpWebRequest : WebRequest    {        //        // Summary:        //     Gets or sets the value of the Accept http header.        //        // Returns:        //     The value of the Accept http header. The default value is null.        public string Accept { get; set; }        //        // Summary:        //     Gets or sets a value that indicates whether to buffer the received from the Internet        //     resource.        //        // Returns:        //     Returns System.Boolean.true to enable buffering of the data received from the        //     Internet resource; false to disable buffering. The default is true.        public virtual bool AllowReadStreamBuffering { get; set; }        public overrIDe string ContentType { get; set; }        //        // Summary:        //     Gets or sets a timeout,in milliseconds,to wait until the 100-Continue is received        //     from the server.        //        // Returns:        //     Returns system.int32.The timeout,to wait until the 100-Continue        //     is received.        public int ContinueTimeout { get; set; }        //        // Summary:        //     Gets or sets the cookies associated with the request.        //        // Returns:        //     A System.Net.cookieContainer that contains the cookies associated with this request.        public virtual cookieContainer cookieContainer { get; set; }        //        // Summary:        //     Gets or sets authentication information for the request.        //        // Returns:        //     An System.Net.ICredentials that contains the authentication credentials associated        //     with the request. The default is null.        public overrIDe ICredentials Credentials { get; set; }        //        // Summary:        //     Gets a value that indicates whether a response has been received from an Internet        //     resource.        //        // Returns:        //     true if a response has been received; otherwise,false.        public virtual bool HaveResponse { get; }        //        // Summary:        //     SpecifIEs a collection of the name/value pairs that make up the http headers.        //        // Returns:        //     A System.Net.WebheaderCollection that contains the name/value pairs that make        //     up the headers for the http request.        //        // Exceptions:        //   T:system.invalIDOperationException:        //     The request has been started by calling the System.Net.httpWebRequest.GetRequestStream,//     System.Net.httpWebRequest.BeginGetRequestStream(System.AsyncCallback,System.Object),//     System.Net.httpWebRequest.GetResponse,or System.Net.httpWebRequest.BeginGetResponse(System.AsyncCallback,System.Object)        //     method.        public overrIDe WebheaderCollection headers { get; set; }        //        // Summary:        //     Gets or sets the method for the request.        //        // Returns:        //     The request method to use to contact the Internet resource. The default value        //     is GET.        //        // Exceptions:        //   T:System.ArgumentException:        //     No method is supplIEd.-or- The method string contains invalID characters.        public overrIDe string Method { get; set; }        //        // Summary:        //     Gets the original Uniform Resource IDentifIEr (URI) of the request.        //        // Returns:        //     A System.Uri that contains the URI of the Internet resource passed to the System.Net.WebRequest.Create(System.String)        //     method.        public overrIDe Uri RequestUri { get; }        //        // Summary:        //     Gets a value that indicates whether the request provIDes support for a System.Net.cookieContainer.        //        // Returns:        //     Returns System.Boolean.true if a System.Net.cookieContainer is supported; otherwise,//     false.        public virtual bool SupportscookieContainer { get; }        //        // Summary:        //     Gets or sets a System.Boolean value that controls whether default credentials        //     are sent with requests.        //        // Returns:        //     true if the default credentials are used; otherwise false. The default value        //     is false.        //        // Exceptions:        //   T:system.invalIDOperationException:        //     You attempted to set this property after the request was sent.        public overrIDe bool UseDefaultCredentials { get; set; }        //        // Summary:        //     Cancels a request to an Internet resource.        public overrIDe voID Abort();        //        // Summary:        //     Begins an asynchronous request for a System.IO.Stream object to use to write        //     data.        //        // Parameters:        //   callback:        //     The System.AsyncCallback delegate.        //        //   state:        //     The state object for this request.        //        // Returns:        //     An System.IAsyncResult that references the asynchronous request.        //        // Exceptions:        //   T:System.Net.ProtocolViolationException:        //     The System.Net.httpWebRequest.Method property is GET or head.-or- System.Net.httpWebRequest.KeepAlive        //     is true,System.Net.httpWebRequest.AllowWriteStreamBuffering is false,System.Net.httpWebRequest.ContentLength        //     is -1,System.Net.httpWebRequest.SendChunked is false,and System.Net.httpWebRequest.Method        //     is POST or PUT.        //        //   T:system.invalIDOperationException:        //     The stream is being used by a prevIoUs call to System.Net.httpWebRequest.BeginGetRequestStream(System.AsyncCallback,System.Object)-or-        //     System.Net.httpWebRequest.TransferEnCoding is set to a value and System.Net.httpWebRequest.SendChunked        //     is false.-or- The thread pool is running out of threads.        //        //   T:System.NotSupportedException:        //     The request cache valIDator indicated that the response for this request can        //     be served from the cache; however,requests that write data must not use the        //     cache. This exception can occur if you are using a custom cache valIDator that        //     is incorrectly implemented.        //        //   T:System.Net.WebException:        //     System.Net.httpWebRequest.Abort was prevIoUsly called.        //        //   T:System.ObjectdisposedException:        //     In a .NET Compact Framework application,a request stream with zero content length        //     was not obtained and closed correctly. For more information about handling zero        //     content length requests,see Network Programming in the .NET Compact Framework.        public overrIDe IAsyncResult BeginGetRequestStream(AsyncCallback callback,object state);        //        // Summary:        //     Begins an asynchronous request to an Internet resource.        //        // Parameters:        //   callback:        //     The System.AsyncCallback delegate        //        //   state:        //     The state object for this request.        //        // Returns:        //     An System.IAsyncResult that references the asynchronous request for a response.        //        // Exceptions:        //   T:system.invalIDOperationException:        //     The stream is already in use by a prevIoUs call to System.Net.httpWebRequest.BeginGetResponse(System.AsyncCallback,System.Object)-or-        //     System.Net.httpWebRequest.TransferEnCoding is set to a value and System.Net.httpWebRequest.SendChunked        //     is false.-or- The thread pool is running out of threads.        //        //   T:System.Net.ProtocolViolationException:        //     System.Net.httpWebRequest.Method is GET or head,and either System.Net.httpWebRequest.ContentLength        //     is greater than zero or System.Net.httpWebRequest.SendChunked is true.-or- System.Net.httpWebRequest.KeepAlive        //     is true,and either        //     System.Net.httpWebRequest.ContentLength is -1,System.Net.httpWebRequest.SendChunked        //     is false and System.Net.httpWebRequest.Method is POST or PUT.-or- The System.Net.httpWebRequest        //     has an entity body but the System.Net.httpWebRequest.BeginGetResponse(System.AsyncCallback,System.Object)        //     method is called without calling the System.Net.httpWebRequest.BeginGetRequestStream(System.AsyncCallback,System.Object)        //     method. -or- The System.Net.httpWebRequest.ContentLength is greater than zero,//     but the application does not write all of the promised data.        //        //   T:System.Net.WebException:        //     System.Net.httpWebRequest.Abort was prevIoUsly called.        public overrIDe IAsyncResult BeginGetResponse(AsyncCallback callback,object state);        //        // Summary:        //     Ends an asynchronous request for a System.IO.Stream object to use to write data.        //        // Parameters:        //   asyncResult:        //     The pending request for a stream.        //        // Returns:        //     A System.IO.Stream to use to write request data.        //        // Exceptions:        //   T:System.ArgumentNullException:        //     asyncResult is null.        //        //   T:System.IO.IOException:        //     The request dID not complete,and no stream is available.        //        //   T:System.ArgumentException:        //     asyncResult was not returned by the current instance from a call to System.Net.httpWebRequest.BeginGetRequestStream(System.AsyncCallback,System.Object).        //        //   T:system.invalIDOperationException:        //     This method was called prevIoUsly using asyncResult.        //        //   T:System.Net.WebException:        //     System.Net.httpWebRequest.Abort was prevIoUsly called.-or- An error occurred        //     while processing the request.        public overrIDe Stream EndGetRequestStream(IAsyncResult asyncResult);        //        // Summary:        //     Ends an asynchronous request to an Internet resource.        //        // Parameters:        //   asyncResult:        //     The pending request for a response.        //        // Returns:        //     A System.Net.WebResponse that contains the response from the Internet resource.        //        // Exceptions:        //   T:System.ArgumentNullException:        //     asyncResult is null.        //        //   T:system.invalIDOperationException:        //     This method was called prevIoUsly using asyncResult.-or- The System.Net.httpWebRequest.ContentLength        //     property is greater than 0 but the data has not been written to the request stream.        //        //   T:System.Net.WebException:        //     System.Net.httpWebRequest.Abort was prevIoUsly called.-or- An error occurred        //     while processing the request.        //        //   T:System.ArgumentException:        //     asyncResult was not returned by the current instance from a call to System.Net.httpWebRequest.BeginGetResponse(System.AsyncCallback,System.Object).        public overrIDe WebResponse EndGetResponse(IAsyncResult asyncResult);    }}

我甚至不知道添加什么细节的问题.请告诉我,我可以.

解决方法 根据MSDN上的文档约 HttpWebRequest.UserAgent Property

这是版本信息

Version Information

> .NET Framework
自1.1以来可用
> Silverlight
自5.0以来可用
> windows Phone Silverlight
自7.0以来可用

你会注意到没有提到与HttpWebRequest Class不同的便携式类库(PCL)

Version Information

>通用windows平台
自4.5起可用
> .NET Framework
自1.1以来可用
>便携式类库
支持:便携式.NET平台
> Silverlight
自2.0以来可用
> windows Phone Silverlight
自7.0以来可用
> windows Phone
自8.1以来可用

所以这就意味着在PCL中不能访问HttpWebRequest.UserAgent Property

现在虽然有可移植类库中有HttpWebRequest.Headers Property,但您应该注意到MSDN中的以下注释

Remarks

The headers collection contains the protocol headers associated with
the request. The following table Lists the http headers that are not
stored in the headers collection but are either set by the system or
set by propertIEs or methods.

然后他们继续在该列表中包含User-Agent头.

The Add method throws an ArgumentException if you try to set one of
these protected headers.

You should not assume that the header values will remain unchanged,
because Web servers and caches may change or add headers to a Web
request.

很可能它是由PCL执行的系统设置的.

Xamarin documentation显示该物业应该存在,但我相信这些信息可能不正确.

System.Net.httpWebRequest.UserAgent PropertyGets or sets the value of the User-agent http header.Syntaxpublic String UserAgent { get; set; }Value    A String containing the value of the http User-agent header. The default value is null.RemarksNote: For additional information see section 14.43 of IETF RFC 2616 - http/1.1.Requirementsnamespace: System.NetAssembly: System (in System.dll)Assembly Versions: 1.0.5000.0,2.0.0.0,4.0.0.0

您应该确认便携式类库定位的平台,以确保不会删除UserAgent属性的冲突.

PCL:支持的类型和成员

便携式类库项目中提供的类型和成员受几个兼容性因素限制:

>他们必须在您选择的目标对象之间共享.
>这些目标的行为必须相似.
>他们不能是弃权的候选人.
>他们在便携式环境中必须有意义,特别是当支持成员不可携带时.

这是一些documentation from Xamarin

更新

我可以使用HttpClient API来设置User-Agent并获取Get请求.

public async Task GetURL(string url) {    var handler = new httpclienthandler();    var httpClIEnt = new httpClIEnt(handler);    httpClIEnt.DefaultRequestheaders.Add("User-Agent","My Custom User Agent");    var request = new httpRequestMessage(httpMethod.Get,url);    var response = await httpClIEnt.SendAsync(request);    //...other code removed for brevity}
总结

以上是内存溢出为你收集整理的c# – 为什么我的HttpWebRequest类中缺少UserAgent属性?全部内容,希望文章能够帮你解决c# – 为什么我的HttpWebRequest类中缺少UserAgent属性?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1262448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存