【问题标题】:Silverlight 4 HttpWebRequest throwing ProtocolViolationExceptionSilverlight 4 HttpWebRequest 抛出 ProtocolViolationException
【发布时间】:2010-07-25 19:15:39
【问题描述】:

我正在通过 http 调用 REST 服务,该服务返回一个流作为响应。 我的客户端代码如下所示:

                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);

我在这一行收到以下错误:

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

System.Net.ProtocolViolationException 未被用户代码处理 由于对象的当前状态,消息=操作无效。 堆栈跟踪: 在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态) 在 System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 LaharSubProxy.SubscribeAsyncResult`1.c__DisplayClass1.b__0(IAsyncResult responseResult) 在 System.Net.Browser.BrowserHttpWebRequest.c__DisplayClassd.b__b(Object state2) 在 System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 在 System.Threading.ThreadPoolWorkQueue.Dispatch() 在 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() 内部异常:

这让我非常抓狂,因为异常没有指向任何有用的东西。我在 fiddler 中启用了“流式传输”选项,但没有看到任何流量/错误。

请帮忙!

【问题讨论】:

    标签: .net silverlight wcf silverlight-4.0 httpwebrequest


    【解决方案1】:

    知道什么,问题出在这一行:

    webRequest.ContentType = "multipart/mixed";
    

    似乎这是一个 SL 错误/细微差别。我评论了那条线,错误消失了! 我还应该补充一点,代码在传统的控制台/Windows 应用程序中运行良好。

    This post 为我指明了正确的方向。谢谢微软!!!!!!

    【讨论】:

      【解决方案2】:

      设置 ContentType 属性意味着您将在请求中提供消息正文。 .net 库似乎不想允许这样做,尽管我在 HTTP 规范中找不到任何建议它应该被禁止的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-25
        • 2017-06-04
        相关资源
        最近更新 更多