HttpWebRequest的使用POST性能 [英] Performance of HttpWebRequest using POST

查看:600
本文介绍了HttpWebRequest的使用POST性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小工具,我用来测试web服务。

I have a small tool I use for testing a webservice.

它可以是使用POST或使用GET调用Web服务。

It can either call the webservice using POST or using GET.

在code使用POST是

The code for using POST is

public void PerformRequest()
{
  WebRequest webRequest = WebRequest.Create(_uri);

  webRequest.ContentType = "application/ocsp-request";
  webRequest.Method = "POST";
  webRequest.Credentials = _credentials;
  webRequest.ContentLength = _request.Length;
  ((HttpWebRequest)webRequest).KeepAlive = false;

  using (Stream st = webRequest.GetRequestStream())
    st.Write(_request, 0, _request.Length);

  using (HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
  using (Stream responseStream = httpWebResponse.GetResponseStream())
  using (BufferedStream bufferedStream = new BufferedStream(responseStream))
  using (BinaryReader reader = new BinaryReader(bufferedStream))
  {
    if (httpWebResponse.StatusCode != HttpStatusCode.OK)
      throw new WebException("Got response status code: " + httpWebResponse.StatusCode);

    byte[] response = reader.ReadBytes((int)httpWebResponse.ContentLength);
    httpWebResponse.Close();
  }      
}

在$ C $下使用得到的是:

The code for using GET is:

protected override void PerformRequest()
{
  WebRequest webRequest = WebRequest.Create(_uri + "/" + Convert.ToBase64String(_request));

  webRequest.Method = "GET";
  webRequest.Credentials = _credentials;
  ((HttpWebRequest)webRequest).KeepAlive = false;

  using (HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
  using (Stream responseStream = httpWebResponse.GetResponseStream())
  using (BufferedStream bufferedStream = new BufferedStream(responseStream))
  using (BinaryReader reader = new BinaryReader(bufferedStream))
  {
    if (httpWebResponse.StatusCode != HttpStatusCode.OK)
      throw new WebException("Got response status code: " + httpWebResponse.StatusCode);

    byte[] response = reader.ReadBytes((int)httpWebResponse.ContentLength);
    httpWebResponse.Close();
  }
}

正如你所看到的code颇为相似。如果有什么事情,我希望一开始的方法是稍微慢一些,因为它要连接code和以Base64传输数据。

As you can see the code is quite similar. If anything, I would expect the GET-method to be slightly slower, as it has to encode and transmit the data in Base64.

但是,当我运行它,我看到POST方法采用远更多的处理能力比一开始的方法。在我的机器,我可以运行Get-方法的使用大约5%的CPU 80线程,而POST方法的80线程使用95%的CPU。

But when I run it, I see that the POST-method uses far more processing power than the GET-method. On my machine, I can run 80 threads of the GET-method using approximately 5% CPU, while 80 threads of the POST-method uses 95% CPU.

有什么本质上有关使用POST更贵?有什么我可以做优化后的方法?我不能重用连接,我想模拟不同客户端的请求。

Is there something inherently more expensive about using POST? Is there anything I can do to optimize the POST-method? I cannot reuse the connection, as I want to simulate requests from different clients.

dotTrace报告说,65%的加工时间都用在webRequest.GetResponse()使用POST的时候。

dotTrace reports that 65% of the processing-time is spent in webRequest.GetResponse() when using POST.

底层的Web服务使用摘要,验证如果有什么差别。

The underlying webservice uses Digest-Authentication if that makes any difference.

推荐答案

嗯,这取决于最终的URI的复杂性,这也许是因为获取请求被缓存? 邮报不是默认缓存,但GET经常是(因为它应该是幂等)。您是否尝试过嗅,看看是否有什么不同吗?

Well, depending on the complexity of the final uri, it might be that the "GET" requests are being cached? "POST" is not cached by default, but "GET" often is (as it should be idempotent). Have you tried sniffing to see if there is any difference here?

另外 - 你可能会发现 Web客户端更容易使用 - 这样的:

Also - you might find WebClient easier to use - something like:

using (WebClient wc = new WebClient())
{
    byte[] fromGet = wc.DownloadData(uriWithData);
    byte[] fromPost = wc.UploadData(uri, data);
}

这篇关于HttpWebRequest的使用POST性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆