views:

1783

answers:

2

Making a request to a RESTful service in Silverlight with HttpWebRequest works well so long as I don't add any headers to the request.

As soon as I add a header using code like this

var webReq = (HttpWebRequest)WebRequest.Create(new Uri(_RemoteAddress, "GetProviderMetadata"));
webReq.Method = HttpMethodType.Get;
webReq.Headers["SomeToken"] = "someTokenValue";

I get the exception pasted at the bottom of this question as soon as EndGetResponse() is called. Anyone know why this is? Adding headers to get requests seems to work fine in normal .NET so I'm guessing it's some sort of Silverlight limitation but I can't find any documentation that clarifies.

   {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"
+1  A: 

Silverlight only supports setting headers using the POST method not the GET method. This is due to a limitation in how the TCP/IP stack is implemented in Silverlight. It uses the browser extension APIs instead of going directly against the host OS's APIs.

chuckj
I was guessing this might be the case. Do you have a link to MSDN docs that explain this limitation? I was not able to find anything.
spoon16
A: 

Hi,

I have same error if I try to set for post http method variables such as

    WebClient client = new WebClient();
    string boundary = "--" + Guid.NewGuid().ToString();

    client.Headers[HttpRequestHeader.ContentType] = "multipart/form-data; boundary=" + boundary.Substring(2);
    client.Headers["Content-Disposition: form-data; name=\"__REQUESTDIGEST\""] = FormDigest; // in this line exception !!!
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    client.DownloadStringAsync(ub.Uri);

Exception: System.ArgumentException was unhandled by user code Message="Specified value has invalid HTTP Header characters.\r\nParameter name: name" StackTrace: at System.Net.ValidationHelper.CheckBadWebHeaderChars(String name, Boolean isHeaderValue) at System.Net.WebHeaderCollection.set_Item(String name, String value) at DC.FileUpload.FileUpload.client_ReceiveDigest(Object sender, DownloadStringCompletedEventArgs e) at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg) InnerException:

What can I do???

oivoodoo
Nothing... read the accepted answer to this post.
spoon16