Skip to content

Instantly share code, notes, and snippets.

@networm
Last active February 8, 2020 06:53
Show Gist options
  • Save networm/7ec57b7b809532b5976145eec857fba2 to your computer and use it in GitHub Desktop.
Save networm/7ec57b7b809532b5976145eec857fba2 to your computer and use it in GitHub Desktop.
C# API WebRequest default connection limit is 2. Use the request.ServicePoint.ConnectionLimit to unlock this limitation
using UnityEngine;
using System.Net;
using System.Threading;
public class TestConnectionLimit : MonoBehaviour
{
void Download(object arg)
{
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
string url = arg as string;
try
{
var request = WebRequest.Create(url) as HttpWebRequest;
// HTTP 1.1 specifies that A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy
// Use the following code to unlock this limitation
// request.ServicePoint.ConnectionLimit = 10;
request.GetResponse();
}
catch (System.Exception e)
{
Debug.LogWarning(e.ToString());
}
finally
{
Debug.Log(url + " elapsedTimeInMilliseconds: " + sw.ElapsedMilliseconds);
}
}
void DownloadAsync(string url)
{
var thread = new Thread(new ParameterizedThreadStart(Download));
thread.Start(url);
}
void Start()
{
DownloadAsync("http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png");
DownloadAsync("http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png");
DownloadAsync("http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png");
DownloadAsync("http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png");
DownloadAsync("http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment