在HttpWebRequest中提交请求后无法执行此操作

时间:2018-09-05 17:24:51

标签: c# asp.net-core httpwebrequest

以下代码出现错误。

HttpWebRequest webRequest = WebRequest.Create(URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
response.Cookies = webRequest.CookieContainer.GetCookies(webRequest.RequestUri); //Error here
string responseData = responseReader.ReadToEnd();
responseReader.Close();
  

错误:System.InvalidOperationException:提交请求后无法执行此操作。      在System.Net.HttpWebRequest.GetResponse()

上面的代码有什么问题?

1 个答案:

答案 0 :(得分:4)

HttpWebResponse.Cookies Property应该已经包含与响应关联的cookie。

您还应避免多次致电drop table if exists #tmp CREATE TABLE #tmp (rnd_val int, weights int) INSERT INTO #tmp VALUES (1001, 54); INSERT INTO #tmp VALUES (2001, 27); INSERT INTO #tmp VALUES (2002, 21); update @tab set ChannelID = null while ((select count(1) from @tab where ChannelID is null) > 0) begin ;with cte as (select top 1 [Date], ChannelID from @tab where ChannelID is null) update cte set ChannelID = (select top 1 rnd_val from #tmp order by RAND(CAST(NEWID() AS VARBINARY)) * weights desc) end select * from @tab

GetResponse

您可以将cookie容器重用于后续请求,因为它们会被响应添加