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