ReadFromStream() public méthode

public ReadFromStream ( Stream inputStream ) : void
inputStream Stream
Résultat void
Exemple #1
0
        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
            }
        }
Exemple #2
0
        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
            }
        }