public override Response ParseNetworkResponse(NetworkResponse response)
 {
     Java.Lang.String parsed;
     try
     {
         parsed = new Java.Lang.String(response.Data, HttpHeaderParser.ParseCharset(response.Headers));
     }
     catch (Java.IO.UnsupportedEncodingException)
     {
         parsed = new Java.Lang.String(response.Data);
     }
     return Response.Success(parsed.ToString(), HttpHeaderParser.ParseCacheHeaders(response));
 }
 public ServerError(NetworkResponse networkResponse)
     : base(networkResponse)
 {
 }
 public override Response ParseNetworkResponse(NetworkResponse response)
 {
     return null;
 }
        public static Entry ParseCacheHeaders(NetworkResponse response)
        {
            long now = SystemClock.CurrentThreadTimeMillis();

            Dictionary<String, String> headers = response.Headers;

            long serverDate = 0;
            long lastModified = 0;
            long serverExpires = 0;
            long softExpire = 0;
            long finalExpire = 0;
            long maxAge = 0;
            long staleWhileRevalidate = 0;
            bool hasCacheControl = false;
            bool mustRevalidate = false;

            String serverEtag = null;
            String headerValue;

            headers.TryGetValue("Date", out headerValue);
            if (headerValue != null)
            {
                serverDate = ParseDateAsEpoch(headerValue);
            }

            headers.TryGetValue("Cache-Control", out headerValue);
            if(headerValue != null)
            {
                hasCacheControl = true;
                String[] tokens = headerValue.Split(',');
                for (int i = 0; i < tokens.Length; i++)
                {
                    String token = tokens[i].Trim();
                    if (token == "no-cache" || token == "no-store")
                    {
                        return null;
                    }
                    else if (token.StartsWith("max-age="))
                    {
                        try
                        {
                            maxAge = long.Parse(token.Substring(8));
                        }
                        catch (Exception) { }
                    }
                    else if (token.StartsWith("stale-while-revalidate="))
                    {
                        try
                        {
                            staleWhileRevalidate = long.Parse(token.Substring(23));
                        }
                        catch (Exception) { }
                    }
                    else if (token == "must-revalidate" || token == "proxy-revalidate")
                    {
                        mustRevalidate = true;
                    }
                }
            }

            headers.TryGetValue("Expires", out headerValue);
            if (headerValue != null)
            {
                serverExpires = ParseDateAsEpoch(headerValue);
            }

            headers.TryGetValue("Last-Modified", out headerValue);
            if (headerValue != null)
            {
                lastModified = ParseDateAsEpoch(headerValue);
            }

            headers.TryGetValue("ETag", out serverEtag);

            if (hasCacheControl)
            {
                softExpire = now + maxAge * 1000;
                finalExpire = mustRevalidate ? softExpire : softExpire + staleWhileRevalidate * 1000;
            }
            else if (serverDate > 0 && serverExpires >= serverDate)
            {
                softExpire = now + (serverExpires - serverDate);
                finalExpire = softExpire;
            }

            Entry entry = new Entry();
            entry.Data = response.Data;
            entry.ETag = serverEtag;
            entry.SoftTtl = softExpire;
            entry.Ttl = finalExpire;
            entry.ServerDate = serverDate;
            entry.LastModified = lastModified;
            entry.ResponseHeaders = headers;

            return entry;
        }
        /// <summary>
        /// ����������
        /// �������ײ�����Ĵ���
        /// </summary>
        public NetworkResponse PerformRequest(Request request)
        {
            long requestStart = SystemClock.ElapsedRealtime();
            while (true)
            {
                HttpWebResponse httpResponse = null;
                byte[] responseContents = null;
                Dictionary<String,String> responseHeaders = null;
                try
                {
                    Dictionary<String, String> headers = new Dictionary<string, string>();
                    AddCacheHeaders(headers, request.CacheEntry);

                    //��������
                    httpResponse = mHttpStack.PerformRequest(request, headers);

                    var statusCode = httpResponse.StatusCode;
                    responseHeaders = ConvertHeaders(httpResponse.Headers);

                    if (statusCode == HttpStatusCode.MovedPermanently || statusCode == HttpStatusCode.Moved)
                    {
                        String newUrl = responseHeaders["Location"];
                        request.SetRedirectUrl(newUrl);
                    }

                    //��ȡ���󵽵�����
                    Stream output = httpResponse.GetResponseStream();
                    if (output != null)
                    {
                        responseContents = EntityToBytes(output);
                    }
                    else
                    {
                        responseContents = new byte[0];
                    }

                    long requestLifetime = SystemClock.ElapsedRealtime() - requestStart;
                    LogSlowRequests(requestLifetime, request, responseContents, statusCode);

                    if (statusCode < HttpStatusCode.OK || (int)statusCode > 299)
                    {
                        throw new IOException();
                    }
                    return new NetworkResponse(statusCode, responseContents, responseHeaders, false,
                        SystemClock.ElapsedRealtime() - requestStart);
                }
                catch (WebException ex)
                {
                    if (ex.Response != null)
                    {
                        var result = ex.Response as HttpWebResponse;
                        if (result.StatusCode == HttpStatusCode.NotModified)
                        {
                            Entry entry = request.CacheEntry;
                            if (entry == null)
                            {
                                return new NetworkResponse(HttpStatusCode.NotModified, null,
                                    responseHeaders, true,
                                    SystemClock.ElapsedRealtime() - requestStart);
                            }
                            //entry.ResponseHeaders = entry.ResponseHeaders.Intersect(responseHeaders).ToDictionary(x => x.Key, x => x.Value);
                            return new NetworkResponse(HttpStatusCode.NotModified, entry.Data,
                                entry.ResponseHeaders, true,
                                SystemClock.ElapsedRealtime() - requestStart);
                        }
                    }
                    throw new NetworkError(ex);
                }
                catch (TimeoutException)
                {
                    AttempRetryOnException("connection", request, new TimeoutError());
                }
                catch (IOException e)
                {
                    HttpStatusCode statusCode = 0;
                    NetworkResponse networkResponse = null;
                    if (httpResponse != null)
                    {
                        statusCode = httpResponse.StatusCode;
                    }
                    else
                    {
                        throw new NoConnectionError(e);
                    }
                    if (statusCode == HttpStatusCode.MovedPermanently)
                    {
                        VolleyLog.E("Request at {0} has been redirected to {1}", request.OriginUrl, request.Url);
                    }
                    else
                    {
                        VolleyLog.E("Unexpected response code {0} for {1}", statusCode, request.Url);
                    }
                    if (responseContents != null)
                    {
                        networkResponse = new NetworkResponse(statusCode, responseContents,
                            responseHeaders, false, SystemClock.ElapsedRealtime() - requestStart);
                        if (statusCode == HttpStatusCode.Unauthorized || statusCode == HttpStatusCode.Forbidden)
                        {
                            AttempRetryOnException("auth", request, new AuthFailureError());
                        }
                        else if (statusCode == HttpStatusCode.MovedPermanently || statusCode == HttpStatusCode.Moved)
                        {
                            AttempRetryOnException("redirect", request, new AuthFailureError(networkResponse));
                        }
                        else
                        {
                            throw new ServerError(networkResponse);
                        }
                    }
                    else
                    {
                        throw new NetworkError(networkResponse);
                    }
                }
            }
        }
 public override Response ParseNetworkResponse(NetworkResponse response)
 {
     return(null);
 }
 public AuthFailureError(NetworkResponse response)
     : base(response)
 {
 }
 public NetworkError(NetworkResponse networkResponse)
     : base(networkResponse)
 {
 }
 public ParseError(NetworkResponse networkResponse)
     : base(networkResponse)
 {
 }
Exemple #10
0
 public abstract Response ParseNetworkResponse(NetworkResponse response);