ReadFromStream() public method

public ReadFromStream ( Stream inputStream, Stream bodyStream ) : void
inputStream Stream
bodyStream Stream
return void
Example #1
0
        void BeginSending()
        {
            isDone = false;

            if (timeout > 0) {
                Scheduler.Instance.StartCoroutine (Timeout ());
            }

            System.Threading.ThreadPool.QueueUserWorkItem (delegate(object state) {

                try {
                    var retryCount = 0;
                    Connection connection = null;

                    while (retryCount < maximumRedirects) {
                        AddHeadersToRequest ();
                        Uri pUri;
                        if(proxy != null)
                            pUri = proxy;
                        else {
                            try {
                                if(System.Net.WebRequest.DefaultWebProxy != null)
                                    pUri = System.Net.WebRequest.DefaultWebProxy.GetProxy(uri);
                                else
                                    pUri = uri;
                            } catch(TypeInitializationException) {
                                //Could not get WebRequest type... default to no proxy.
                                pUri = uri;
                            }

                        }

                        connection = CreateConnection (pUri.Host, pUri.Port, pUri.Scheme.ToLower () == "https");

                        WriteToStream (connection.stream);

                        response = new Response (this);

                        try {

                            response.ReadFromStream (connection.stream, bodyStream);

                        } catch (HTTPException) {

                            retryCount ++;
                            continue;
                        }

                        connectionPool[pUri.Host] = connection;
                        if (enableCookies) {
                            foreach (var i in response.headers.GetAll("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;
                            break;
                        case 304:
                            break;
                        case 307:
                            uri = new Uri (response.headers.Get ("Location"));
                            retryCount ++;
                            continue;
                        case 302:
                        case 301:
                            method = "GET";
                            var location = response.headers.Get ("Location");
                            if(location.StartsWith("/")) {
                                uri = new Uri(uri, location);
                            } else {
                                uri = new Uri (location);
                            }

                            retryCount ++;
                            continue;
                        default:
                            break;
                        }
                        break;
                    }

                    if (upgradedConnection == null) {
                        if(response.protocol.ToUpper() == "HTTP/1.0" || response.headers.Get("Connection").ToUpper() == "CLOSE") {
                            if(connectionPool.ContainsKey(uri.Host)) {
                                connectionPool.Remove(uri.Host);
                            }
                            connection.Dispose ();
                        }
                    }

                    if (useCache && response != null) {
                        var etag = response.headers.Get ("etag");
                        if (etag.Length > 0) {
                            etags [uri.AbsoluteUri] = etag;
                        }
                    }
                } catch (Exception e) {

                    exception = e;
                    response = null;
                }

                isDone = true;
            });
        }