c# - HttpWebRequest 在 mono 和 .net 中的实现方式不同吗?

标签 c# mono stream httpwebrequest cloudinary

我正在尝试将 c# cloudinary api 移植到单声道,但在构建 http 请求时遇到了一些问题。

我已经分离出这种设置请求的方法,但 HttpWebRequest.ContentLength 结果在单声道中为 -1,但 .net 内容已正确构建。

我正在 Mac 上运行最新的 Xamarin Studio,并且正在构建一个 MONO/.NET 4.0 库 单声道版本:2.10.12

编辑:简化代码,此测试在 Visual Studio 中通过但在 Xamarin studio 中失败

编辑:代码被推送到 github如果有人愿意帮忙

    [Test]
    public void StreamTest()
    {
        var request = System.Net.HttpWebRequest.Create("http://foo.com");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        using (var writer = new System.IO.StreamWriter(request.GetRequestStream()))
        {
            writer.Write("anything");
        }

        Assert.IsTrue(request.ContentLength > 0);
    }

最佳答案

当你这样做的时候

System.Net.HttpWebRequest.Create("http://foo.com");

你实际上创建了一个内部类的实例System.Net.Browser.BrowserHttpWebRequest

继承层次结构

System.Net.WebRequest
System.Net.HttpWebRequest
System.Net.Browser.PolicyBasedWebRequest
System.Net.Browser.BrowserHttpWebRequest

内容长度在 PolicyBasedWebRequest 中处理,在 ctor 中用 -1 初始化并且永远不会改变; 我建议你手动设置它。

关于c# - HttpWebRequest 在 mono 和 .net 中的实现方式不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16482956/

相关文章:

c# - SQL Azure 数据库的本地 SQL 更改

c# - C#中灵活的Logging接口(interface)设计

android - 在 Mac 上使用 Jenkins 构建 Xamarin Android 项目错误 MSB3073

c# - 在 Windows 上使用 mkbundle 创建 C# 可执行文件

c# - 如何使用进度回调将文件下载到字符串中?

java - 从 URL 解析 Pdf、txt 或 docx 文件中的文本,无需在 Java 8 中下载

c# - SQLiteConnection.BeginTransaction() 已弃用

c# - 创建时的 Kubernetes 节点状态

c# - 从 C 调用 Mono exe 找不到 libc

c++ - 如何编写带有自定义标志的 ostream 运算符