放置HTTP Post请求时,Stream不支持读取

时间:2015-04-28 10:31:42

标签: c# http-post filestream

我遇到的错误是" Stream不支持阅读"。我正在向网址发送一个Http帖子请求。以下是我正在使用的代码

var request = (HttpWebRequest) WebRequest.Create("https://test.com/Hotel Hospitality Source?method=fetchInfo");

var postData = "&username=testing";
postData += "&password=Testing";
postData += "&hotelId=h075-103";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using(var ms = new MemoryStream())
request.GetRequestStream().CopyTo(ms);

var response = (HttpWebResponse) request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

首先,我使用了以下代码。

 using (var stream = request.GetRequestStream())
 {
     stream.Write(data, 0, data.Length);
 }

 var response = (HttpWebResponse)request.GetResponse();

 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

enter image description here

我在使用CopyTo方法进行流时遇到错误。如何解决此错误?

1 个答案:

答案 0 :(得分:4)

UPDATE tbl_test SET tag = case when DBInstance = 'INS1' and DBName = 'master' then 'a' when DBInstance = 'INS2' and DBName = 'tempdb' then 'b' else NULL -- or may be tag, or default value which you want. end 应该做什么?您正在尝试将请求流(只能写入)复制到您随后处理的新内存流中。

您需要写入请求流:

using (var ms=new MemoryStream()) request.GetRequestStream().CopyTo(ms);