【问题标题】:How can I set Headers with Silverlight GET HttpWebRequest?如何使用 Silverlight GET HttpWebRequest 设置标头?
【发布时间】:2009-03-08 02:14:34
【问题描述】:

只要我不向请求中添加任何标头,使用 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"

【问题讨论】:

    标签: c# .net silverlight httpwebrequest


    【解决方案1】:

    在调用您发起 WebRequest 之前指定 "Client HTTP" handling

    像这样:

    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 没有。
    【解决方案2】:

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

    【讨论】:

    • 我猜可能是这种情况。您是否有指向 MSDN 文档的链接来解释此限制?我找不到任何东西。
    • 我能找到的最接近的内容是此页面底部的列表:msdn.microsoft.com/en-us/library/… 某些标头根据方法和 URI 受到限制。
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多