Example #1
0
        void BeginSending()
        {
            isDone = false;

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

            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) {
                try {
                    var retryCount            = 0;
                    HttpConnection connection = null;

                    while (retryCount < maximumRedirects)
                    {
                        AddHeadersToRequest();
                        Uri pUri;
                        if (proxy != null)
                        {
                            pUri = proxy;
                        }
                        else
                        {
#if UNITY_WP8
                            pUri = uri;
#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;
                            }
#endif
                        }

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

                        WriteToStream(connection.stream);


                        response = new Response(this);
                        try {
                            response.ReadFromStream(connection.stream);
                        } catch (HTTPException) {
                            retryCount++;
                            continue;
                        }
#if USE_KEEPALIVE
                        connectionPool[pUri.Host] = connection;
#endif

#if USE_COOKIES
                        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.
                                }
                            }
                        }
#endif

                        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 USE_KEEPALIVE
                        if (response.protocol.ToUpper() == "HTTP/1.0" || response.headers.Get("Connection").ToUpper() == "CLOSE")
                        {
                            if (connectionPool.ContainsKey(uri.Host))
                            {
                                connectionPool.Remove(uri.Host);
                            }
                            connection.Dispose();
                        }
#else
                        connection.Dispose();
#endif
                    }

                    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;
            }));
        }