I'm posting an XML string to a port on an AIX box. I have two ways in which I'm connecting to this box (TcpClient & HttpWebRequest). I have timers in place to give me an idea how long it is taking the AIX box to process my request and respond.
It appears that the TcpClient is faster than the HttpWebRequest by up to 100 milliseconds. I suspect that my timer locations may be incorrect. Either way, I don't think the timer location would amount for such a big difference in response time.
Another thought I had was the using statements. Perhaps they are keeping the connection open longer than the TcpClient.
Is the TcpClient approach known to be faster?
// TcpClient
TcpClient client = new TcpClient(host, port);
DateTime x = DateTime.Now;
NetworkStream stream = client.GetStream();
NetworkStream stream = client.GetStream();
stream.Write(request, 0, request.Length);
stream.Flush();
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
response.Append(encoder.GetString(buffer, 0, count));
DateTime y = DateTime.Now;
totalMS = y.Subtract(x).TotalMilliseconds;
// HttpWebRequest
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URI);
using (Stream webStream = webRequest.GetRequestStream())
{
webStream.Write(postdata, 0, postdata.Length);
webStream.Close();
DateTime x = DateTime.Now;
using (WebResponse webresponse = webRequest.GetResponse())
{
webresponse.Close();
DateTime y = DateTime.Now;
using (Stream rs = webresponse.GetResponseStream())
{
using (StreamReader sr = new StreamReader(rs, Encoding.Default))
{
// Read response to end
}
}
}
}
totalMS = y.Subtract(x).TotalMilliseconds;