【问题标题】:Stream does not support seek operationsStream 不支持查找操作
【发布时间】:2015-01-21 19:13:56
【问题描述】:

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

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

【问题讨论】:

  • 为什么不改用HttpClient呢?
  • 附带说明一下,您像这样使用String.Format 似乎毫无意义......

标签: c# httpwebrequest http-post


【解决方案1】:

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

如果您想在流中查找相同的数据,请考虑将整个当前流读取到byte[] 缓冲区,然后从该缓冲区中构造一个MemoryStream 以与您的StreamReader 一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2014-06-30
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 2015-11-27
    相关资源
    最近更新 更多