【问题标题】:How to ignore reponse data from when send Post request?发送 Post 请求时如何忽略响应数据?
【发布时间】:2012-03-15 08:28:32
【问题描述】:

我通过以下代码发送 Post 请求:

try
            {
                string responseContent;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = cookieContainer;
                // Set Method to "POST"
                request.Method = "POST";
                // Set the content type of the WebRequest
                request.ContentType = "application/x-www-form-urlencoded";
                request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
                // Set the content length
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] byteArray = encoding.GetBytes(requestCommand);
                request.ContentLength = byteArray.Length;
                // Get the request stream
                using (Stream requestStream = request.GetRequestStream())
                {
                    // Write the "POST" data to the stream
                    requestStream.Write(byteArray, 0, byteArray.Length);
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (BufferedStream buffer = new BufferedStream(responseStream))
                        {
                            using (StreamReader reader = new StreamReader(buffer))
                            {
                                responseContent = reader.ReadToEnd();
                            }
                        }
                    }
                }
                return responseContent;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
}

一切正常。但是下面的代码行太慢了。

using (StreamReader reader = new StreamReader(buffer))
{
   responseContent = reader.ReadToEnd();
}

我不知道为什么!我花了更多时间寻找解决方案,例如 set proxy = null,... 但没有结果。 有什么办法可以忽略那条线。我不需要接收响应数据。我已尝试将这些行替换为:

using (Stream responseStream = response.GetResponseStream())
{
   responseStream.Flush();
   responseStream.Close();
}

但我无法正确且成功地发送 Post 请求。请帮我。非常感谢!

【问题讨论】:

    标签: c# .net httpwebrequest streamreader httpwebresponse


    【解决方案1】:

    如果您根本不关心响应或它是否失败,您可以将响应排队到一个新线程上并忽略它。

    using (Stream requestStream = request.GetRequestStream())
    {
        // Write the "POST" data to the stream
        requestStream.Write(byteArray, 0, byteArray.Length);
    }
    
    // now put the get response code in a new thread and immediately return
    ThreadPool.QueueUserWorkItem((x) =>
    {
        using (var objResponse = (HttpWebResponse) request.GetResponse())
        {
            responseStream = new MemoryStream();
            objResponse.GetResponseStream().CopyTo(responseStream);
            responseStream.Seek(0, SeekOrigin.Begin);
         }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 2020-09-30
      • 1970-01-01
      • 2020-06-09
      • 2012-05-13
      • 2017-11-08
      • 1970-01-01
      • 2014-07-13
      • 2011-03-03
      相关资源
      最近更新 更多