Exemplo n.º 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
                            if (System.Net.WebRequest.DefaultWebProxy != null)
                            {
                                pUri = System.Net.WebRequest.DefaultWebProxy.GetProxy(uri);
                            }
                            else
                            {
                                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;
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 开始发送请求数据
        /// </summary>
        void BeginSending()
        {
            isDone = false;

            if (timeout > 0)
            {
                //开启超时协议检测
                UniWeb.Instance.StartCoroutine(Timeout());
            }
            //使用线程池请求数据
            System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
            {
                try
                {
                    //当前尝试重度的次数
                    var retryCount            = 0;
                    HttpConnection connection = null;

                    while (retryCount < maximumRedirects)
                    {
                        exception = null;
                        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;
                            }
                        }

                        response = new Response(this);

                        try
                        {
                            connection = CreateConnection(pUri.Host, pUri.Port, pUri.Scheme.ToLower() == "https");
#if BNICKSON_UPDATED
                            WriteToStreamAsync(connection.stream);
#else
                            WriteToStream(connection.stream);
#endif
                            response.ReadFromStream(connection.stream, bodyStream);
                        }
                        catch (HTTPException ex)
                        {
                            EB.Debug.LogWarning("HTTPException {0}, retry ...", ex.Message);
                            exception = ex;
                            retryCount++;
                            continue;
                        }
                        catch (ObjectDisposedException ex)
                        {
                            EB.Debug.LogWarning("ObjectDisposedException {0}, retry ...", ex.Message);
                            exception = ex;
                            retryCount++;
                            continue;
                        }
                        catch (SocketException ex)
                        {
                            EB.Debug.LogWarning("SocketException {0}, retry ...", ex.Message);
                            exception = ex;
                            retryCount++;
                            continue;
                        }

                        string key          = string.Format("{0}:{1}", pUri.Host, pUri.Port);
                        connectionPool[key] = 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;
                        }

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

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