当我意识到它丢失时,我试图将值设置为来自 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);
}
}
我什至不知道要在问题中添加什么细节。请告诉我,这样我就可以了。
最佳答案
根据 MSDN 上关于 HttpWebRequest.UserAgent Property 的文档
这是版本信息
您会注意到,与 HttpWebRequest Class 不同,这里没有提到可移植类库 (PCL)。
所以这意味着访问 HttpWebRequest.UserAgent Property在 PCL 中不可用
现在虽然有一个HttpWebRequest.Headers Property在可移植类库中可用,您应该注意 MSDN 的以下注释
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 header 。
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 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
您应该确认可移植类库面向的平台,以确保不存在会删除 UserAgent 属性的冲突。
PCL:支持的类型和成员
可移植类库项目中可用的类型和成员受几个兼容性因素的限制:
它们必须在您选择的目标之间共享。
必须在这些目标中表现相似。
它们不能成为弃用的候选对象。
它们必须在可移植环境中有意义,尤其是当支持成员不可移植时。
这是一些 documentation from Xamarin
更新
我能够使用 HttpClient用于设置 User-Agent 并发出 Get 请求的 API。
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 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38593271/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串