Silverlight 4 中的 HttpWebRequest

发布于 2024-12-17 04:37:11 字数 1512 浏览 1 评论 0原文

我正在尝试创建 httpwebrequest 到一个 url (REST API),我正在其中将流写入目标 api 服务器。但在写入流之前,在我的请求对象中:用户代理抛出错误,'request.UserAgent' 抛出'System.NotImplementedException' 类型的异常。即使我也有硬编码的 useragent 值。其他两个参数 AllowAutoRedirectCookieContainer 的情况相同。另一方面,所有其他参数都具有正确的值或为空。

对此有何帮助,为什么 UserAgent 参数抛出此错误 'request.UserAgent' 抛出 'System.NotImplementedException' 类型的异常。以下是我的网络请求:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("TargetAPIIUrl_I am passing here"));

        request.Method = "POST";
        string boundary = "---------------" + DateTime.Now.Ticks.ToString();
        string formDataBoundary = "-----------------------------" + DateTime.Now.Ticks.ToString().Substring(0, 14);
        string contentType = "multipart/form-data; boundary=" + formDataBoundary;
        request.ContentType = contentType;
        request.UserAgent = "Hardcoded string of my target API";
         request.BeginGetRequestStream(new AsyncCallback(asyncResult =>
        {
            Stream stream = request.EndGetRequestStream(asyncResult);

            SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer ser = new         SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer(boundary);
            ser.WriteObject(stream, parameters);
            stream.Close();
            request.BeginGetResponse(callback, request);
        }), request);

I am trying to create httpwebrequest to one url (REST API) where i am writing stream to target api server. But before writing stream, in my request object : User Agent is throwing error that 'request.UserAgent' threw an exception of type 'System.NotImplementedException'. Even i have hard coded useragent value also. Same case with other two params AllowAutoRedirect and CookieContainer. On the other hand all other params having correct value or null.

Any help on this, why UserAgent param is throwing this error 'request.UserAgent' threw an exception of type 'System.NotImplementedException'. Below is my web request:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("TargetAPIIUrl_I am passing here"));

        request.Method = "POST";
        string boundary = "---------------" + DateTime.Now.Ticks.ToString();
        string formDataBoundary = "-----------------------------" + DateTime.Now.Ticks.ToString().Substring(0, 14);
        string contentType = "multipart/form-data; boundary=" + formDataBoundary;
        request.ContentType = contentType;
        request.UserAgent = "Hardcoded string of my target API";
         request.BeginGetRequestStream(new AsyncCallback(asyncResult =>
        {
            Stream stream = request.EndGetRequestStream(asyncResult);

            SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer ser = new         SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer(boundary);
            ser.WriteObject(stream, parameters);
            stream.Close();
            request.BeginGetResponse(callback, request);
        }), request);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2024-12-24 04:37:11

UserAgentAllowAutoRedirect 属性的存在是为了与 .NET 框架 HttpWebRequest 保持一定的一致性,但是 ClientHTTP 和 BrowserHTTP 实现都不支持它们。

可以将 CookieContainer 与 ClientHTTP 堆栈一起使用,BrowserHTTP 堆栈当然将使用主机浏览器 cookie 管理。

The UserAgent and AllowAutoRedirect properties are present to maintain some consistency with the .NET framework HttpWebRequest however neither the ClientHTTP nor the BrowserHTTP implementations support them.

It is possible to use a CookieContainer with the ClientHTTP stack, the BrowserHTTP stack will of course use the host browser cookie management.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文