M
mwieder
I developed an application that automates dwonloading of a web page
hundreds of times. I first developed it by using http web request, but
then I read up that HTTP Web Request has some overhead that I thought I
could eliminate if I re-wrote it with sockets. However, what I have
found is that sockets actually works significantly slower (I get about
1/2 to 2/3 of the performance I get with HTTPWebRequest).
Now, the web server sends a close command on every request so even if I
post a Keep-Alive request I have to create and open a new socket for
every request, but I assume the HTTPRequest class must do the same
thing under the covers. Below is my Socket Send Method (very basic).
Can anyone explain this strange disparity in performance?
public string Send(string strRequest)
{
m_sock = new Socket(ipe.AddressFamily, SocketType.Stream,
ProtocolType.Tcp);
m_sock.Connect(ipe);
string strRetPage = null;
Encoding ASCII = Encoding.ASCII;
Byte[] ByteGet = ASCII.GetBytes(HttpRequest);
Byte[] RecvBytes = new Byte[256];
m_sock.Send(ByteGet, ByteGet.Length, 0);
Int32 bytes;
do
{
bytes = m_sock.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
} while (bytes > 0);
m_sock.Shutdown(SocketShutdown.Both);
m_sock.Close();
return strRetPage;
}
hundreds of times. I first developed it by using http web request, but
then I read up that HTTP Web Request has some overhead that I thought I
could eliminate if I re-wrote it with sockets. However, what I have
found is that sockets actually works significantly slower (I get about
1/2 to 2/3 of the performance I get with HTTPWebRequest).
Now, the web server sends a close command on every request so even if I
post a Keep-Alive request I have to create and open a new socket for
every request, but I assume the HTTPRequest class must do the same
thing under the covers. Below is my Socket Send Method (very basic).
Can anyone explain this strange disparity in performance?
public string Send(string strRequest)
{
m_sock = new Socket(ipe.AddressFamily, SocketType.Stream,
ProtocolType.Tcp);
m_sock.Connect(ipe);
string strRetPage = null;
Encoding ASCII = Encoding.ASCII;
Byte[] ByteGet = ASCII.GetBytes(HttpRequest);
Byte[] RecvBytes = new Byte[256];
m_sock.Send(ByteGet, ByteGet.Length, 0);
Int32 bytes;
do
{
bytes = m_sock.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
} while (bytes > 0);
m_sock.Shutdown(SocketShutdown.Both);
m_sock.Close();
return strRetPage;
}