为什么我的HttpWebRequest类缺少UserAgent属性?

nic*_*cks 10 .net c# xamarin xamarin.forms

当我意识到它缺失时,我试图将值设置为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
#endregion

using 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, in milliseconds, 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, System.Net.HttpWebRequest.AllowWriteStreamBuffering is false, 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Nko*_*osi 12

根据MSDN上关于HttpWebRequest.UserAgent属性的文档

这是版本信息

版本信息

  • .NET Framework 自1.1起可用
  • Silverlight 自5.0起可用
  • Windows Phone Silverlight 自7.0起可用

您会注意到,与HttpWebRequest类不同,没有提及可移植类库(PCL)

版本信息

  • 通用Windows平台 自4.5起可用
  • .NET Framework 自1.1起可用
  • 可移植类库 支持:可移植.NET平台
  • Silverlight 自2.0起可用
  • Windows Phone Silverlight 自7.0起可用
  • Windows Phone 自8.1起可用

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

现在虽然可移植类库中有一个HttpWebRequest.Headers属性,但您应该注意MSDN的以下评论

备注

Headers集合包含与请求关联的协议标头.下表列出了未存储在Headers集合中但由系统设置或由属性或方法设置的HTTP标头.

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

如果您尝试设置其中一个受保护的标头,Add方法将抛出ArgumentException.
...
您不应该认为标头值将保持不变,因为Web服务器和缓存可能会更改标头或向Web请求添加标头.

最有可能的是,它是由PCL正在执行的系统设置的.

Xamarin文档显示该属性应该存在,但我相信这些信息可能不正确.

System.Net.HttpWebRequest.UserAgent Property

Gets or sets the value of the User-agent HTTP header.
Syntax
public String UserAgent { get; set; }
Value

    A String containing the value of the HTTP User-agent header. The default value is null.

Remarks
Note: For additional information see section 14.43 of IETF RFC 2616 - HTTP/1.1.
Requirements
Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0
Run Code Online (Sandbox Code Playgroud)

您应该确认可移植类库所针对的平台,以确保没有可以删除该UserAgent属性的冲突.

PCL:支持的类型和成员

可移植类库项目中可用的类型和成员受到多个兼容性因素的约束:

  • 必须在您选择的目标之间共享它们.

  • 必须在这些目标上表现相似.

  • 他们不能成为弃用的候选人.

  • 它们必须在便携式环境中有意义,特别是当支持成员不便携时.

以下是Xamarin的一些文档

更新

我能够使用HttpClientAPI来设置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
}
Run Code Online (Sandbox Code Playgroud)


Igo*_*gor 6

我不清楚你是想尝试添加它还是检索它,所以我会在你指定你正在做的事情后更新我的答案.

用户代理将包含在Http Header中.您包含的类具有一个名为property的属性Headers,该属性应该被找到/设置.

用于添加:该类型WebHeaderCollection具有字符串索引器,该字符串索引器将标题字段作为键.您应该能够添加它或从那里检索它.

request.Headers["User-Agent"] = "your user agent string";
Run Code Online (Sandbox Code Playgroud)

对于检索,您可以使用字符串索引器.

var userAgent = request.Headers["User-Agent"];
Run Code Online (Sandbox Code Playgroud)

在这里,您可以找到WebHeaderCollection类型的源代码

  • @NikaGamkrelidze - 看看Nikosi发布的答案.虽然乍看之下我认为这是可能显然你不能设置`用户Agent`的`HttpWebRequest`,你应该使用`HttpClient`代替.Nikosi也有,为什么很好的解释. (2认同)