C# 如何使用Silverlight GET HttpWebRequest设置标题?
使用HttpWebRequest向Silverlight中的RESTful服务发出请求,只要不向请求添加任何头,就可以正常工作 只要我用这样的代码添加一个头C# 如何使用Silverlight GET HttpWebRequest设置标题?,c#,.net,silverlight,httpwebrequest,C#,.net,Silverlight,Httpwebrequest,使用HttpWebRequest向Silverlight中的RESTful服务发出请求,只要不向请求添加任何头,就可以正常工作 只要我用这样的代码添加一个头 var webReq = (HttpWebRequest)WebRequest.Create(new Uri(_RemoteAddress, "GetProviderMetadata")); webReq.Method = HttpMethodType.Get; webReq.Headers["SomeToken"] = "someToke
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"
{System.NotSupportedException-->System.NotSupportedException:不支持指定的方法。
在System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)上
在System.Net.BrowserHttpWebRequest.c__DisplayClass5.b__4(对象发送状态)中
在System.Net.AsyncHelper.c__DisplayClass2.b__0(对象发送状态)中
---内部异常堆栈跟踪的结束---
位于System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态)
位于System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
在System.Net.BrowserHttpWebRequest.c__DisplayClassd.b__b(对象状态2)中
在System.Threading._ThreadPoolWaitCallback.WaitCallback_上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallbackInternal(\u ThreadPoolWaitCallback tpWaitCallBack)
在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallback(对象状态)}
[System.NotSupportedException]:{System.NotSupportedException-->System.NotSupportedException:不支持指定的方法。
在System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)上
在System.Net.BrowserHttpWebRequest.c__DisplayClass5.b__4(对象发送状态)中
在System.Net.AsyncHelper.c__DisplayClass2.b__0(对象发送状态)中
---内部异常堆栈跟踪的结束---
位于System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态)
位于System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
在System.Net.BrowserHttpWebRequest.c__DisplayClassd.b__b(对象状态2)中
在System.Threading._ThreadPoolWaitCallback.WaitCallback_上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallbackInternal(\u ThreadPoolWaitCallback tpWaitCallBack)
在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallback(对象状态)}
_类名:“System.NotSupportedException”
_数据:{System.Collections.ListDictionaryInternal}
_dynamicMethods:null
_例外方法:null
_exceptionMethodString:null
_帮助URL:null
_HResult:-2146233067
_innerException:{System.NotSupportedException:不支持指定的方法。
在System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)上
在System.Net.BrowserHttpWebRequest.c__DisplayClass5.b__4(对象发送状态)中
在System.Net.AsyncHelper.c__DisplayClass2.b__0(对象发送状态)}
_信息:“
_remoteStackIndex:0
_remoteStackTraceString:空
_资料来源:“System.Windows”
_堆栈跟踪:{sbyte[192]}
_stackTraceString:空
_xcode:-532462766
_xptrs:0
数据:{System.Collections.ListDictionaryInternal}
帮助链接:空
HResult:-2146233067
InnerException:{System.NotSupportedException:不支持指定的方法。
在System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)上
在System.Net.BrowserHttpWebRequest.c__DisplayClass5.b__4(对象发送状态)中
在System.Net.AsyncHelper.c__DisplayClass2.b__0(对象发送状态)}
信息:“
资料来源:“System.Windows”
StackTrace:“位于System.Net.AsyncHelper.BeginUnui(SendOrPostCallback beginMethod,对象状态)\r\n位于System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n位于Intellidimension.RdfEntity.Service.RemoteEntityServiceProviderClient.getProviderMetadataResponse(IAsyncResult结果)\r\n在System.Net.BrowserHttpWebRequest.c_uuDisplayClassd.b_uub(对象状态2)\r\n在System.Threading.\u ThreadPoolWaitCallback.WaitCallback\u Context(对象状态)\r\n在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback callback,对象状态)\r\n在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallbackInternal(\u ThreadPoolWaitCallback tpWaitCallBack)\r\n在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallback(对象状态)
System.Runtime.InteropServices.\u Exception.HelpLink:null
System.Runtime.InteropServices.\u Exception.Source:“System.Windows”
Silverlight仅支持使用POST方法而不是GET方法设置标题。这是由于在Silverlight中实现TCP/IP堆栈的方式受到限制。它使用浏览器扩展API,而不是直接与主机操作系统的API对抗。在调用之前指定,然后启动WebRequest
像这样:
bool httpResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
我猜可能是这样的。您是否有解释此限制的MSDN文档链接?我找不到任何内容。我能找到的最接近的内容是本页底部的列表:某些标题根据方法和URI受到限制。我已验证使用ClientHttp允许在GET请求上设置标题,尽管我将每个WebRequest创建为ClientHttp,如下所示:
var request=(HttpWebRequest)System.Net.Browser.WebRequestCreator.ClientHttp.Create(端点)
BrowserHttp限制GET请求的头(如前面的回答所述),但ClientHttp不限制。