c# - Stream不支持seek操作

标签 c# httpwebrequest http-post

我正在尝试发布到一个网站,其中唯一需要的参数是“描述”。我相信这与我编码数据的方式有关。我是否错过了什么/以错误的方式处理这个问题?

string postData = String.Format("description=" + description + "&");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Method = WebRequestMethods.Http.Post;
wr.ContentLength = byteArray.Length;
wr.ContentType = "application/x-www-form-urlencoded";

Stream postStream = wr.GetRequestStream();
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();

WebResponse response = await wr.GetResponseAsync();
HttpWebResponse webResponse = (HttpWebResponse)response;
using (var stm = response.GetResponseStream())
{
    using (var reader = new StreamReader(stm))
    {
        var content = await reader.ReadToEndAsync();
        reader.Close();
        stm.Close();
        return content;
    }
}

最佳答案

您尝试读取的流显然不支持查找。您还可以通过其 CanSeek 以编程方式验证这一点。属性。

如果您希望在流中查找相同的数据,请考虑将整个当前流读取到 byte[] 缓冲区,然后构建 MemoryStream从该缓冲区中取出供您的 StreamReader 使用。

关于c# - Stream不支持seek操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075003/

相关文章:

c# - 让 POST 端点在自托管 (WebServiceHost) C# web 服务中工作?

C# HttpWebRequest/Response 给出 400 Bad Request。但 Firefox HTTP 资源测试则不然

c# - 如何像 HttpWebRequest 一样使用 TcpClient 执行 HTTPS?

java - 如何在 Android 中使用 POST 方法发布任何数据

c# - WIndows 8.1 应用程序中缺少 System.Environment 定义

browser - 当用户在其网络浏览器中按下停止按钮时会发生什么?

java - 我在来自 Java 客户端的 Http Post 请求中收到无效参数错误

c# - 独立项目中的 ASP.NET 5 Web 项目和 Entity Framework

c# - EF Core 3.1 ExecuteSqlRaw/ExecuteSqlRawAsync 是否是 ExecuteSqlCommand/ExecuteSqlCommandAsync 的直接替代品?

c# - 字典 : An item with the same key has already been added