代码之家  ›  专栏  ›  技术社区  ›  Eric Schoonover thSoft

如何使用Silverlight设置头GET HTTPWebRequest?

  •  3
  • Eric Schoonover thSoft  · 技术社区  · 15 年前

    使用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 回复  |  直到 10 年前
        1
  •  3
  •   chuckj    15 年前

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

        2
  •  5
  •   Paul Martin    13 年前

    指定 "Client HTTP" handling 在你打电话之前,你启动你的网络请求。

    这样地:

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