I'm downloading a file from an HTTP server that has to be requested using POST data. The download completes, and when I target .NET CORE 2.1, it downloads a 1 MB file in around 50 msec. However, I'm developing for an application targeting .NET Framework 4.7.1, and when I run the EXACT same code targeting that framework (in a brand new empty project even), instead of 50 msec, it takes roughly 1200 times longer to download the EXACT same file from the EXACT same server. The file does eventually download successfully, but it takes far too long.
Looking at Wireshark data, I can see the when targeting either framework, the server sends the data mostly as packets with 1300 bytes of payload per packet. When targeting .NET CORE 2.1, it sends all of the packets in rapid succession. When targeting .NET Framework 4.7.1, it rapidly sends exactly 12 packets containing 1300 bytes and one packet containing 784 bytes (totaling exactly 16384 bytes, or 2^14), then it sends nothing for about 1 second, then it sends another burst of 16384 bytes of data, then it pauses for about 1 second, and so forth until the entire file has been sent.
What am I doing wrong? Since I know the server is capable of sending all of the packets in rapid succession, what do I need to change about my request to make it happen?
This is my code:
Uri address = new Uri("http://servername/file.cgi");
HttpWebRequest request = WebRequest.CreateHttp(address);
string postData = "filename=/folder/filename.xml&Download=Download";
var data = Encoding.ASCII.GetBytes(postData);
request.Credentials = new NetworkCredential("myusername", "mypassword");
request.Method = "POST";
request.KeepAlive = true;
request.UserAgent = "ThisApp";
request.Accept = "text/xml";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream output = File.OpenWrite("ReceivedFile.xml"))
using (Stream input = response.GetResponseStream())
{
input.CopyTo(output);
}
Thanks!!
Also, I've already tried several things I've found on other posts (this one was particularly relevant: HttpWebRequest is extremely slow!), but none of them have helped:
ServicePointManager.DefaultConnectionLimit = 200;
ServicePointManager.Expect100Continue = false;
request.Proxy = null;
request.SendChunked = true;
request.ServicePoint.ReceiveBufferSize = 999999;
I've also tried adding this to app.config:
<connectionManagement>
<add address="*" maxconnection="200"/>
</connectionManagement>