SoapHttpClientProtocol与HttpWebRequest

时间:2009-11-04 17:31:48

标签: asp.net

我正在进行一些性能测试,遇到一些令我不解的事情,我希望有人可以解决一些问题。

我正在比较HttpWebRequest和SoapHttpClientProtocol之间的性能。在我的测试中,我看到SoapHttpClientProtocol类的执行速度是原来的两倍。但是,我希望HttpWebRequest的性能更好。

感谢任何人提供的任何见解!

萨姆

以下是HttpWebRequest的代码

public string RetrieveValue()
{
    ASCIIEncoding encoding = new ASCIIEncoding();

    byte[] payload = encoding.GetBytes("sIP=");

    string Url = @"url/RetrieveValue";

    HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(Url);
    wr.Method = "POST";
    wr.KeepAlive = false;
    wr.ContentType = "application/x-www-form-urlencoded";
    wr.ContentLength = payload.Length;
    wr.Timeout = 30000;

    HttpWebResponse webResponse;

    Stream wrStream = wr.GetRequestStream();

    wrStream.Write(payload, 0, payload.Length);
    wrStream.Close();

    webResponse = (HttpWebResponse)wr.GetResponse();

    Stream baseStream = webResponse.GetResponseStream();

    string result = null;

    using (StreamReader sr = new StreamReader(baseStream))
        result = sr.ReadToEnd();

    return result;
}

以下是SoapHttpClientProtocol的代码

WebServiceBinding(Name = "Soap", Namespace = "http://namespace.com/")] 
public class MyRetriever : SoapHttpClientProtocol
{
    [SoapDocumentMethod("http://url.com/Retrieve", RequestNamespace = "http://url.com/", ResponseNamespace = "http://url.com/", Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
    public string RetrieveValue(string sVal)
    {
        return (string)base.Invoke("RetrieveValue",
                                   new object[] { sVal })[0];
    }
}

1 个答案:

答案 0 :(得分:0)

你是如何调用这两个测试的? RetrieveValue每次都建立一个新的连接,如果您使用的是测试soap客户端的单个实例,并且每次可能不会产生相同的开销时调用GetNewSessionKey。