Silverlight 4 HttpWebRequest throwing ProtocolViolationException

旧巷老猫 提交于 2019-12-12 17:16:16

问题


I'm invoking a REST service via http which returns a stream as the response. My client code looks like so:

                Uri uri = new Uri(remoteAddress);
                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.Method = "GET";
                webRequest.ContentType = "multipart/mixed";
                webRequest.BeginGetResponse(responseResult =>
                {
                    HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(responseResult);
                    this.messages = ParseResponse(response);
                    Complete(false);
                }, null);

I'm getting the following error on this line:

 HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(responseResult);

System.Net.ProtocolViolationException was unhandled by user code Message=Operation is not valid due to the current state of the object. StackTrace: at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at LaharSubProxy.SubscribeAsyncResult`1.<>c__DisplayClass1.b__0(IAsyncResult responseResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassd.b__b(Object state2) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() InnerException:

This is driving me absolutely nuts since the exception is not pointing to anything useful. I have enabled the "streaming" option in fiddler but I'm not seeing any traffic/errors.

Please help!


回答1:


Well whatdya know, the problem is this line:

webRequest.ContentType = "multipart/mixed";

Seems like this is a SL bug/nuance. I commented that line out and the error disappears! I should also add that the code as is works just fine in a traditional console/windows app.

This post pointed me the right direction. Thanks Microsoft!!!!!!




回答2:


Setting the ContentType property implies that you are going to provide a message body with your request. It would seem that the .net libraries don't want to allow this, though I can find nothing in the HTTP spec to suggest that it should be fobidden.



来源:https://stackoverflow.com/questions/3330638/silverlight-4-httpwebrequest-throwing-protocolviolationexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!