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.LogException (e); return; } } WriteToStream (ostream); response = new KnetikResponse (); response.request = this; state = KnetikRequestState.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) { Debug.LogException(e); Console.WriteLine ("Unhandled Exception, aborting request."); Console.WriteLine (e); exception = e; response = null; } state = KnetikRequestState.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 KnetikInitializationScript.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( Action<KnetikRequest> callback ) { completedCallback = callback; isDone = false; state = KnetikRequestState.Waiting; if (acceptGzip) SetHeader ("Accept-Encoding", "gzip"); if ( this.cookieJar != null ) { List< KnetikCookie > cookies = this.cookieJar.GetCookies( new KnetikCookieAccessInfo( uri.Host, uri.AbsolutePath ) ); string cookieString = this.GetHeader( "cookie" ); for ( int cookieIndex = 0; cookieIndex < cookies.Count; ++cookieIndex ) { if ( cookieString.Length > 0 && cookieString[ cookieString.Length - 1 ] != ';' ) { cookieString += ';'; } cookieString += cookies[ cookieIndex ].name + '=' + cookies[ cookieIndex ].value + ';'; } SetHeader( "cookie", cookieString ); } if ( bytes != null && bytes.Length > 0 && GetHeader ("Content-Length") == "" ) { SetHeader( "Content-Length", bytes.Length.ToString() ); } /* if ( GetHeader( "User-Agent" ) == "" ) { SetHeader( "User-Agent", "UnityWeb 1.0 ( Unity " + Application.unityVersion + " ) ( " + SystemInfo.operatingSystem + " )" ); } */ if ( GetHeader( "Connection" ) == "" ) { SetHeader( "Connection", "close" ); } // Basic Authorization if (!String.IsNullOrEmpty(uri.UserInfo)) { SetHeader("Authorization", "Basic " + System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(uri.UserInfo))); } if (synchronous) { GetResponse(); } else { ThreadPool.QueueUserWorkItem (new WaitCallback ( delegate(object t) { GetResponse(); })); } }