public void Send(string filePath = null, bool onlyContentLength = false) { isDone = false; state = RequestState.Waiting; if (acceptGzip) { SetHeader("Accept-Encoding", "gzip"); } //ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) { try { var retry = 0; while (++retry < maximumRetryCount) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } SetHeader("Host", uri.Host); #if UNITY_WEBPLAYER /* * string ip = Dns.GetHostAddresses(uri.Host)[0].ToString(); * if (!Security.PrefetchSocketPolicy (ip, 1843)) { * Debug.LogError("Security Exception. Policy file load failed!"); * }*/ #endif var client = new TcpClient(); client.SendTimeout = 5000; client.ReceiveTimeout = 5000; client.Connect(uri.Host, uri.Port); using (var stream = client.GetStream()) { var ostream = stream as Stream; if (uri.Scheme.ToLower() == "https") { ostream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate)); try { var ssl = ostream as SslStream; ssl.AuthenticateAsClient(uri.Host); } catch (Exception e) { Debug.LogError("Exception: " + e.Message); return; } } WriteToStream(ostream); if (filePath != null) { response = new Response(filePath, onlyContentLength); } else { response = new Response(); } if (downloadDelegate != null) { response.downloadDelegate = downloadDelegate; } state = RequestState.Reading; response.ReadFromStream(ostream); } client.Close(); switch (response.status) { case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); continue; default: retry = maximumRetryCount; break; } } if (useCache) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags[uri.AbsoluteUri] = etag; } } } catch (SocketException e) { response.status = -100; if (downloadDelegate != null) { IOErrorEvent ioErrorEvent = new IOErrorEvent(); ioErrorEvent.ErrorCode = e.ErrorCode; ioErrorEvent.Message = e.Message; downloadDelegate.OnIOError(ioErrorEvent); } Debug.LogException(e); } catch (IOException ioex) { response.status = -100; if (downloadDelegate != null) { IOErrorEvent ioErrorEvent = new IOErrorEvent(); ioErrorEvent.Message = ioex.Message; downloadDelegate.OnIOError(ioErrorEvent); } Debug.LogException(ioex); } catch (Exception e) { Console.WriteLine("Unhandled Exception, aborting request."); Console.WriteLine(e); Debug.Log(e.Message + " Type: " + e.GetType().FullName); exception = e; response = null; } //Debug.Log ("Request finished"); state = RequestState.Done; isDone = true; //})); }
private void GetResponse() { System.Diagnostics.Stopwatch curcall = new System.Diagnostics.Stopwatch(); curcall.Start(); try { var retry = 0; while (++retry < maximumRetryCount) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } SetHeader("Host", uri.Host); var client = new TcpClient(); client.Connect(uri.Host, uri.Port); using (var stream = client.GetStream()) { var ostream = stream as Stream; if (uri.Scheme.ToLower() == "https") { ostream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate)); try { var ssl = ostream as SslStream; ssl.AuthenticateAsClient(uri.Host); } catch (Exception e) { #if !UNITY_EDITOR Console.WriteLine("SSL authentication failed."); Console.WriteLine(e); #else Debug.LogError("SSL authentication failed."); Debug.LogException(e); #endif return; } } WriteToStream(ostream); response = new Response(); response.request = this; state = RequestState.Reading; response.ReadFromStream(ostream); } client.Close(); switch (response.status) { case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); continue; default: retry = maximumRetryCount; break; } } if (useCache) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags[uri.AbsoluteUri] = etag; } } } catch (Exception e) { #if !UNITY_EDITOR Console.WriteLine("Unhandled Exception, aborting request."); Console.WriteLine(e); #else Debug.LogError("Unhandled Exception, aborting request."); Debug.LogException(e); #endif exception = e; response = null; } state = RequestState.Done; isDone = true; responseTime = curcall.ElapsedMilliseconds; if (byteStream != null) { byteStream.Close(); } if (completedCallback != null) { if (synchronous) { completedCallback(this); } else { // we have to use this dispatcher to avoid executing the callback inside this worker thread ResponseCallbackDispatcher.Singleton.requests.Enqueue(this); } } if (LogAllRequests) { #if !UNITY_EDITOR System.Console.WriteLine("NET: " + InfoString(VerboseLogging)); #else if (response != null && response.status >= 200 && response.status < 300) { Debug.Log(InfoString(VerboseLogging)); } else if (response != null && response.status >= 400) { Debug.LogError(InfoString(VerboseLogging)); } else { Debug.LogWarning(InfoString(VerboseLogging)); } #endif } }
public void Send() { isDone = false; state = RequestState.Waiting; if (acceptGzip) SetHeader ("Accept-Encoding", "gzip"); ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { SetHeader ("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) hostHeader += ":" + uri.Port.ToString (); SetHeader ("Host", hostHeader); ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient (uri.Host, uri.Port, uri.Scheme.ToLower () == "https"); } catch (Exception e) { exception = e; response = null; break; } try { WriteToStream (connection.stream); } catch (IOException e) { exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } response = new Response (this); state = RequestState.Reading; try { response.ReadFromStream (connection.stream); } catch (IOException e) { exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri (response.GetHeader ("Location")); if (OnRedirect != null) { OnRedirect (uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //place the connection back into the pool if not upgraded. if(upgradedConnection == null) { if (response.GetHeader ("Connection").ToLower () != "keep-alive") { connection.stream.Close (); } activeConnections [connection.key].Add (connection); } } if (useCache) { string etag = response.GetHeader ("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags (); } } } catch (Exception e) { exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
private void GetResponse() { System.Diagnostics.Stopwatch curcall = new System.Diagnostics.Stopwatch(); curcall.Start(); try { var retry = 0; while (++retry < maximumRetryCount) { if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { SetHeader ("If-None-Match", etag); } } SetHeader ("Host", uri.Host); var client = new TcpClient (); client.Connect (uri.Host, uri.Port); using (var stream = client.GetStream ()) { var ostream = stream as Stream; if (uri.Scheme.ToLower() == "https") { ostream = new SslStream (stream, false, new RemoteCertificateValidationCallback (ValidateServerCertificate)); try { var ssl = ostream as SslStream; ssl.AuthenticateAsClient (uri.Host); } catch (Exception e) { Debug.LogError ("Exception: " + e.Message); return; } } WriteToStream( ostream ); response = new Response (); response.request = this; state = RequestState.Reading; response.ReadFromStream( ostream ); } client.Close (); switch (response.status) { case 307: case 302: case 301: uri = new Uri (response.GetHeader ("Location")); continue; default: retry = maximumRetryCount; break; } } if (useCache) { string etag = response.GetHeader ("etag"); if (etag.Length > 0) etags[uri.AbsoluteUri] = etag; } } catch (Exception e) { Console.WriteLine ("Unhandled Exception, aborting request."); Console.WriteLine (e); exception = e; response = null; } state = RequestState.Done; isDone = true; responseTime = curcall.ElapsedMilliseconds; if ( completedCallback != null ) { if (synchronous) { completedCallback(this); } else { // we have to use this dispatcher to avoid executing the callback inside this worker thread ResponseCallbackDispatcher.Singleton.requests.Enqueue( this ); } } if ( LogAllRequests ) { #if !UNITY_EDITOR System.Console.WriteLine("NET: " + InfoString( VerboseLogging )); #else if ( response != null && response.status >= 200 && response.status < 300 ) { Debug.Log( InfoString( VerboseLogging ) ); } else if ( response != null && response.status >= 400 ) { Debug.LogError( InfoString( VerboseLogging ) ); } else { Debug.LogWarning( InfoString( VerboseLogging ) ); } #endif } }
public void Send () { isDone = false; state = RequestState.Waiting; if (acceptGzip) SetHeader ("Accept-Encoding", "gzip"); ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) { try { var retry = 0; while (++retry < maximumRetryCount) { if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { SetHeader ("If-None-Match", etag); } } SetHeader ("Host", uri.Host); var client = new TcpClient (); client.Connect (uri.Host, uri.Port); using (var stream = client.GetStream ()) { var ostream = stream as Stream; if (uri.Scheme.ToLower() == "https") { ostream = new SslStream (stream, false, new RemoteCertificateValidationCallback (ValidateServerCertificate)); try { var ssl = ostream as SslStream; ssl.AuthenticateAsClient (uri.Host); } catch (Exception e) { Debug.LogError ("Exception: " + e.Message); return; } } WriteToStream (ostream); response = new Response (); state = RequestState.Reading; response.ReadFromStream(ostream); } client.Close (); switch (response.status) { case 307: case 302: case 301: uri = new Uri (response.GetHeader ("Location")); continue; default: retry = maximumRetryCount; break; } } if (useCache) { string etag = response.GetHeader ("etag"); if (etag.Length > 0) etags[uri.AbsoluteUri] = etag; } } catch (Exception e) { Console.WriteLine ("Unhandled Exception, aborting request."); Console.WriteLine (e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
private void GetResponse() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); try { int num = 0; while (++num < maximumRetryCount) { if (useCache) { string value = ""; if (etags.TryGetValue(uri.AbsoluteUri, out value)) { SetHeader("If-None-Match", value); } } SetHeader("Host", uri.Host); TcpClient tcpClient = new TcpClient(); tcpClient.Connect(uri.Host, uri.Port); using (NetworkStream networkStream = tcpClient.GetStream()) { Stream stream = networkStream; if (uri.Scheme.ToLower() == "https") { stream = new SslStream(networkStream, leaveStreamOpen: false, ValidateServerCertificate); try { SslStream sslStream = stream as SslStream; sslStream.AuthenticateAsClient(uri.Host); } catch (Exception ex) { UnityEngine.Debug.LogError("Exception: " + ex.Message); return; } } WriteToStream(stream); response = new Response(); response.request = this; state = RequestState.Reading; response.ReadFromStream(stream); } tcpClient.Close(); switch (response.status) { case 301: case 302: case 307: uri = new Uri(response.GetHeader("Location")); break; default: num = maximumRetryCount; break; } } if (useCache) { string value = response.GetHeader("etag"); if (value.Length > 0) { etags[uri.AbsoluteUri] = value; } } } catch (Exception ex) { Console.WriteLine("Unhandled Exception, aborting request."); Console.WriteLine(ex); exception = ex; response = null; } state = RequestState.Done; isDone = true; responseTime = stopwatch.ElapsedMilliseconds; if (completedCallback != null) { if (synchronous) { completedCallback(this); } else { ResponseCallbackDispatcher.Singleton.requests.Enqueue(this); } } if (LogAllRequests) { Console.WriteLine("NET: " + InfoString(VerboseLogging)); } }
public void Send() { isDone = false; state = RequestState.Waiting; if (acceptGzip) { SetHeader("Accept-Encoding", "gzip"); } ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) { hostHeader += ":" + uri.Port.ToString(); } SetHeader("Host", hostHeader); ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient(uri.Host, uri.Port, uri.Scheme.ToLower() == "https"); } catch (Exception e) { exception = e; response = null; break; } try { WriteToStream(connection.stream); } catch (IOException e) { exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } response = new Response(this); state = RequestState.Reading; try { response.ReadFromStream(connection.stream); } catch (IOException e) { exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); if (OnRedirect != null) { OnRedirect(uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //place the connection back into the pool if not upgraded. if (upgradedConnection == null) { if (response.GetHeader("Connection").ToLower() != "keep-alive") { connection.stream.Close(); } activeConnections [connection.key].Add(connection); } } if (useCache) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags(); } } } catch (Exception e) { exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
public void Send() { if (sent) { throw new InvalidOperationException("Request has already completed."); } sent = true; isDone = false; state = RequestState.Waiting; #if USE_GZIP if (acceptGzip) { SetHeader("Accept-Encoding", "gzip"); } #endif ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) { hostHeader += ":" + uri.Port.ToString(); } SetHeader("Host", hostHeader); if (enableCookies && uri != null) { try { var c = cookies.GetCookieHeader(uri); if (c != null && c.Length > 0) { SetHeader("Cookie", c); } } catch (NullReferenceException) { //Some cookies make the .NET cookie class barf. MEGH again. //Debug.Log (".NET cannot parse this cookie: " + e.ToString ()); } catch (IndexOutOfRangeException) { //Another weird exception that comes through from the cookie class. } } ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient(uri.Host, uri.Port, uri.Scheme.ToLower() == "https"); } catch (Exception e) { Debug.Log(e); exception = e; response = null; break; } try { WriteToStream(connection.stream); } catch (IOException e) { Debug.Log(e); exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } response = new Response(this); state = RequestState.Reading; try { response.ReadFromStream(connection.stream); } catch (IOException e) { Debug.Log(e); exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } if (response != null) { if (enableCookies) { foreach (var i in response.GetHeaders("Set-Cookie")) { try { cookies.SetCookies(uri, i); } catch (System.Net.CookieException) { //Some cookies make the .NET cookie class barf. MEGH. } } } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); if (OnRedirect != null) { OnRedirect(uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //close the connection back if not upgraded. if (upgradedConnection == null) { lock (connectionPool) { var close = response.GetHeader("Connection").ToLower() == "close"; if (!close) { connectionPool.Add(connection); } else { connection.stream.Close(); } } } } } if (useCache && response != null) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags(); } } } catch (Exception e) { Debug.Log(e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
public void Send() { isDone = false; state = RequestState.Waiting; if (acceptGzip) { SetHeader("Accept-Encoding", "gzip"); } ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { try { var retry = 0; while (++retry < maximumRetryCount) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } SetHeader("Host", uri.Host); var client = new TcpClient(); client.Connect(uri.Host, uri.Port); using (var stream = client.GetStream()) { var ostream = stream as Stream; if (uri.Scheme.ToLower() == "https") { ostream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate)); try { var ssl = ostream as SslStream; ssl.AuthenticateAsClient(uri.Host); } catch (Exception e) { Debug.LogError("Exception: " + e.Message); return; } } WriteToStream(ostream); response = new Response(); state = RequestState.Reading; response.ReadFromStream(ostream); } client.Close(); switch (response.status) { case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); continue; default: retry = maximumRetryCount; break; } } if (useCache) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags[uri.AbsoluteUri] = etag; } } } catch (Exception e) { Console.WriteLine("Unhandled Exception, aborting request."); Console.WriteLine(e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
public void Send () { if (sent) throw new InvalidOperationException ("Request has already completed."); sent = true; isDone = false; state = RequestState.Waiting; #if USE_GZIP if (acceptGzip) SetHeader ("Accept-Encoding", "gzip"); #endif ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { SetHeader ("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) hostHeader += ":" + uri.Port.ToString (); SetHeader ("Host", hostHeader); if (enableCookies && uri != null) { try { var c = cookies.GetCookieHeader (uri); if (c != null && c.Length > 0) { SetHeader ("Cookie", c); } } catch (NullReferenceException) { //Some cookies make the .NET cookie class barf. MEGH again. //Debug.Log (".NET cannot parse this cookie: " + e.ToString ()); } catch (IndexOutOfRangeException) { //Another weird exception that comes through from the cookie class. } } ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient (uri.Host, uri.Port, uri.Scheme.ToLower () == "https"); } catch (Exception e) { Debug.Log (e); exception = e; response = null; break; } try { WriteToStream (connection.stream); } catch (IOException e) { Debug.Log (e); exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } response = new Response (this); state = RequestState.Reading; try { response.ReadFromStream (connection.stream); } catch (IOException e) { Debug.Log (e); exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } if (response != null) { if (enableCookies) { foreach (var i in response.GetHeaders("Set-Cookie")) { try { cookies.SetCookies (uri, i); } catch (System.Net.CookieException) { //Some cookies make the .NET cookie class barf. MEGH. } } } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri (response.GetHeader ("Location")); if (OnRedirect != null) { OnRedirect (uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //close the connection back if not upgraded. if (upgradedConnection == null) { lock (connectionPool) { var close = response.GetHeader ("Connection").ToLower () == "close"; if (!close) { connectionPool.Add (connection); } else { connection.stream.Close (); } } } } } if (useCache && response != null) { string etag = response.GetHeader ("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags (); } } } catch (Exception e) { Debug.Log (e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
private void SendFunction(System.Object t) { Debug.Log("SendFunction 0 "); try { var retry = 0; Debug.Log("Send 1 "); while (++retry < maximumRetryCount) { Debug.Log("Send 2 "); if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { SetHeader ("If-None-Match", etag); } } Debug.Log("Send 3 "); SetHeader ("Host", uri.Host); var client = new TcpClient (); client.Connect (uri.Host, uri.Port); Debug.Log("Send 4 "); using (var stream = client.GetStream ()) { Debug.Log("Send 5 "); var ostream = stream as Stream; if (uri.Scheme.ToLower() == "https") { Debug.Log("Send 6 "); ostream = new SslStream (stream, false, new RemoteCertificateValidationCallback (ValidateServerCertificate)); try { var ssl = ostream as SslStream; ssl.AuthenticateAsClient (uri.Host); } catch (Exception e) { Debug.LogError ("Exception: " + e.Message); return; } } Debug.Log("Send 7 "); WriteToStream (ostream); response = new Response (); state = RequestState.Reading; response.ReadFromStream(ostream); } Debug.Log("Send 8 "); client.Close (); switch (response.status) { case 307: case 302: case 301: uri = new Uri (response.GetHeader ("Location")); continue; default: retry = maximumRetryCount; break; } Debug.Log("Send 9 "); } Debug.Log("Send 10 "); if (useCache) { string etag = response.GetHeader ("etag"); if (etag.Length > 0) etags[uri.AbsoluteUri] = etag; } Debug.Log("Send 11 "); } catch (Exception e) { Debug.Log("Send 12 "); Debug.LogException(e); Debug.LogError(e.Message); exception = e; response = null; } Debug.Log("Send 13 "); state = RequestState.Done; isDone = true; }