c# - Silverlight 5 + Internet Explorer 9 在后续 GET 中使用来自 POST 的旧内容类型?

标签 c# .net silverlight silverlight-5.0

我们在工作中遇到了一个非常烦人的问题。在我们的 Silverlight 应用程序进行登录 POST 之后,我们发送的下一个 GET 失败并出现 NotFoundException。这仅在使用 Silverlight 5 运行时时发生,并且仅在 Internet Explorer 9 中发生。我现在已经使用 Silverlight 4 以及 Chrome、Firefox、Opera 和 Safari 进行了测试,IE9/SL5 是唯一的糟糕组合。

这是我们登录 POST 的 header :

POST /login HTTP/1.1<br/> Accept: application/xml<br/> Referer: <a href="http://localhost:8080/censored.xap?timestamp=1326148328000" rel="noreferrer noopener nofollow">http://localhost:8080/censored.xap?timestamp=1326148328000</a><br/> Accept-Language: en-CA<br/> Content-Length: 38<br/> Content-Type: application/x-www-form-urlencoded<br/> Accept-Encoding: gzip, deflate<br/> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)<br/> Host: localhost:8080<br/> Connection: Keep-Alive<br/> Pragma: no-cache<br/> Cookie: JSESSIONID=DEFEAFD35E9B067A79F772C166937750

这是我们下一个 GET 的 header :

GET /user/current HTTP/1.1<br/> Accept: application/xml<br/> Referer: <a href="http://localhost:8080/censored.xap?timestamp=1326148328000" rel="noreferrer noopener nofollow">http://localhost:8080/censored.xap?timestamp=1326148328000</a><br/> Accept-Language: en-CA<br/> Content-Length: 38<br/> Content-Type: application/x-www-form-urlencoded<br/> Accept-Encoding: gzip, deflate<br/> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)<br/> Host: localhost:8080<br/> Proxy-Connection: Keep-Alive<br/> Pragma: no-cache<br/> Cookie: JSESSIONID=5C57C93458E80E5975470F703B4A483C<br/>

请注意 Content-Type 和 Content-Length 是相同的,即使:

  • 不允许在 GET 上使用 Content-Type 或 Content-Length
  • GET请求中的content length其实是不一样的,显然是旧值

还有其他人遇到这个吗?

更新: 它仅在使用 BrowserHttp 时发生。

这是一些示例客户端代码:

using System;
using System.IO;
using System.Net;
using System.Net.Browser;
using System.Windows.Controls;

namespace NetworkTest
{
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        HttpWebRequest request = (HttpWebRequest)WebRequestCreator.BrowserHttp.Create(new Uri("http://localhost:59050/Home/Login"));
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.BeginGetRequestStream(result =>
            {
                var rq = result.AsyncState as HttpWebRequest;

                using (var stream = rq.EndGetRequestStream(result))
                {
                    StreamWriter writer = new StreamWriter(stream);
                    writer.WriteLine("username=test&password=test");
                    writer.Flush();
                    writer.Close();
                }

                rq.BeginGetResponse(
                    r =>
                    {
                        try
                        {
                            rq.EndGetResponse(r);
                        }
                        catch
                        {
                        }
                    }, rq);
            }, request);
    }
}
}

示例服务器代码(来自 ASP MVC 3):

using System.Web.Mvc;

namespace TestServer.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string test)
        {
            return RedirectToAction("Index");
        }
    }
}

您最终会看到一个 POST 登录,这是一个成功的请求。您将被重定向回索引页面,并且 GET 仍然包含来自 POST 的 Content-Type 和 Content-Length,因此失败。

完整的解决方案可在 http://www.mikecousins.com/files/NetworkTest.zip 获得

最佳答案

它对我有用。

这是我对您的项目完成的步骤。

  1. 设置特定端口 59050,因为您对 URL 进行了硬编码但使用了自动分配的端口。
  2. 将 Silverlight 项目添加到 WebServer(从 Casini 运行比从文件运行更好。)
  3. 如下更改 _Layout.cshtml 以添加 SL 对象

    @ViewBag.Title 网络测试 html, 正文 { 高度:100%; 溢出:自动; } body { 填充:0; margin :0; } #silverlightControlHost { 高度:100%; 文本对齐:居中; } 函数 onSilverlightError(sender, args) { var appSource = ""; 如果(发件人!= null && 发件人!= 0){ appSource = sender.getHost().Source;

            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;
    
            if (errorType == "ImageError" || errorType == "MediaError") {
                return;
            }
    
            var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";
    
            errMsg += "Code: " + iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";
    
            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " + args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }
    
            throw new Error(errMsg);
        }
    </script>
    

    我的 MVC 应用程序

    @Html.Partial("_LogOnPartial")
  4. @Html.ActionLink("主页", "索引", "主页")
  5. @Html.ActionLink("关于", "关于", "主页")
  6. @RenderBody()

4.在Web选项卡中勾选“Silverlight”以启用Silverlight调试。 5. 将断点放在 MainPage.xaml.cs 中的 rq.EndGetResponse()(第 34 行)处。 6. 运行应用程序(注意:这个断点会被命中。)

我正在使用 SL5 和 IE9 进行测试。

关于c# - Silverlight 5 + Internet Explorer 9 在后续 GET 中使用来自 POST 的旧内容类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796951/

相关文章:

.net - 从 MS SQL 数据库获取 XML 模式

c# - 从 Silverlight 中的不同 ResourceDictionary 引用 ResourceDictionary 中的资源

c# - 为什么 NavigationService.Navigate 只在最后运行?

c# - 如果失败,如何让webclient重新下载文件?

c# - 寻找 mainviewmodel 上 MVVM 文本 block 绑定(bind)的指导

c# - 如何使用 Selenium WebDriver 和 C# 单击复选框

.net - 公开的嵌套类型

c# - 如何在 c#.net windows 应用程序中检查 gridview 的行是否被选中

wpf - 表达式混合合并路径函数

c# - Delphi 4 和/或 Delphi 5 可执行文件能否集成到 C# 应用程序中?