如何使用Silverlight GET HttpWebRequest设置头信息?

3
使用HttpWebRequest在Silverlight中向RESTful服务发送请求是有效的,只要不添加任何请求头就可以了。
一旦我使用以下代码添加请求头,问题就出现了:
var webReq = (HttpWebRequest)WebRequest.Create(new Uri(_RemoteAddress, "GetProviderMetadata"));
webReq.Method = HttpMethodType.Get;
webReq.Headers["SomeToken"] = "someTokenValue";

当调用EndGetResponse()时,我会立即得到贴在本问题底部的异常。有人知道为什么会这样吗?在正常的.NET中,向请求添加头似乎工作正常,所以我猜这是某种Silverlight限制,但我找不到任何澄清的文档。

   {System.NotSupportedException ---> System.NotSupportedException: Specified method is not supported.
  at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
  at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
  at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
  --- End of inner exception stack trace ---
  at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
  at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
  at System.Net.BrowserHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2)
  at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)}
   [System.NotSupportedException]: {System.NotSupportedException ---> System.NotSupportedException: Specified method is not supported.
  at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
  at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
  at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
  --- End of inner exception stack trace ---
  at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
  at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
  at System.Net.BrowserHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2)
  at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)}
   _className: "System.NotSupportedException"
   _data: {System.Collections.ListDictionaryInternal}
   _dynamicMethods: null
   _exceptionMethod: null
   _exceptionMethodString: null
   _helpURL: null
   _HResult: -2146233067
   _innerException: {System.NotSupportedException: Specified method is not supported.
  at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
  at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
  at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)}
   _message: ""
   _remoteStackIndex: 0
   _remoteStackTraceString: null
   _source: "System.Windows"
   _stackTrace: {sbyte[192]}
   _stackTraceString: null
   _xcode: -532462766
   _xptrs: 0
   Data: {System.Collections.ListDictionaryInternal}
   HelpLink: null
   HResult: -2146233067
   InnerException: {System.NotSupportedException: Specified method is not supported.
  at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
  at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
  at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)}
   Message: ""
   Source: "System.Windows"
   StackTrace: "   at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)\r\n   at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n   at Intellidimension.RdfEntity.Service.RemoteEntityServiceProviderClient.getProviderMetadataResponse(IAsyncResult result)\r\n   at System.Net.BrowserHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2)\r\n   at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)\r\n   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)"
   System.Runtime.InteropServices._Exception.HelpLink: null
   System.Runtime.InteropServices._Exception.Source: "System.Windows"
2个回答

5

在发起 WebRequest 请求之前,请先指定"客户端 HTTP"处理。

如下所示:

bool httpResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

我已验证使用ClientHttp允许在GET请求中设置标头,尽管我像这样将每个WebRequest创建为ClientHttp:var request = (HttpWebRequest)System.Net.Browser.WebRequestCreator.ClientHttp.Create(endpoint); BrowserHttp限制了GET请求的标头(如先前的答案所述),但ClientHttp没有。 - Steve Nay

3

Silverlight只支持使用POST方法设置标头,而不支持使用GET方法。这是由于Silverlight中TCP/IP协议栈的实现方式存在限制。它使用浏览器扩展API而不是直接访问主机操作系统的API。


2
我猜可能是这种情况。你有MSDN文档的链接可以解释这个限制吗?我找不到任何相关的资料。 - Eric Schoonover
我能找到的最接近的东西是此页面底部的列表:https://msdn.microsoft.com/en-us/library/system.net.webheadercollection(VS.95).aspx 根据方法和URI,某些标头受到限制。 - Steve Nay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接