示例#1
0
        internal ClientHttpWebResponse(HttpWebRequest request, object response)
        {
            try {
                content_length = (long)get_content_length.Invoke(response, null);
                content_type   = (string)get_content_type.Invoke(response, null);
                response_uri   = (Uri)get_response_uri.Invoke(response, null);

                SetMethod((string)get_method.Invoke(response, null));

                SetStatus((HttpStatusCode)(int)get_status_code.Invoke(response, null),
                          (string)get_status_description.Invoke(response, null));

                jar     = new CookieCollection();
                headers = new WebHeaderCollection();
                object   header_collection = get_headers.Invoke(response, null);
                string[] keys = ClientReflectionHelper.GetHeaderKeys(header_collection);
                foreach (string key in keys)
                {
                    switch (key)
                    {
                    case "Set-Cookie":
                        string[] values = ClientReflectionHelper.GetHeaderValues(header_collection, key);
                        foreach (string cookie in values)
                        {
                            SetCookie(cookie);
                        }
                        break;

                    default:
                        string value = ClientReflectionHelper.GetHeader(header_collection, key);
                        headers.SetHeader(key, value);
                        break;
                    }
                }

                using (Stream response_stream = (Stream)get_response_stream.Invoke(response, null)) {
                    MemoryStream ms = new MemoryStream();
                    response_stream.CopyTo(ms);
                    stream = new InternalWebResponseStreamWrapper(ms, request.AllowReadStreamBuffering);
                }

                (response as IDisposable).Dispose();
            }
            catch (TargetInvocationException tie) {
                throw tie.InnerException;
            }
        }
        public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
        {
            // under SL the callback MUST call the EndGetResponse, so having no callback is BAD
            // this also means that faking a synch op using EndGetReponse(BeginGetReponse(null,null)) does NOT work
            if (callback == null)
            {
                throw new NotSupportedException();
            }

            // copy Method, Cookies and Headers to System.dll's HttpWebRequest

            try {
                if (Credentials != null)
                {
                    NetworkCredential nc = Credentials.GetCredential(RequestUri, String.Empty);
                    set_credentials.Invoke(request,
                                           ClientReflectionHelper.BuildCredentials(nc.UserName, nc.Password, nc.Domain));
                }

                if ((CookieContainer != null) && (CookieContainer.Count > 0))
                {
                    string cookieHeader = CookieContainer.GetCookieHeader(RequestUri);
                    if (!String.IsNullOrEmpty(cookieHeader))
                    {
                        ClientReflectionHelper.SetHeader(headers, "Cookie", cookieHeader);
                    }
                }

                if (ContentType != null)
                {
                    set_content_type.Invoke(request, new object [] { ContentType });
                }

                if (Headers.Count > 0)
                {
                    foreach (string key in Headers)
                    {
                        // we cannot set some hedaers using the collection
                        switch (key.ToLowerInvariant())
                        {
                        case "accept":
                        case "content-type":
                            // ignore, we already have used the properties Accept and ContentType
                            // and reflect the values to the client stack
                            break;

                        case "range":
                            // XXX inconsistent results (DownloadStringAsync versus OpenReadAsync) in SL
#if false
                            int from, to;
                            if (ParseRange(Headers [key], out from, out to))
                            {
                                add_range.Invoke(request, new object [] { from, to });
                            }
#endif
                            break;

                        default:
                            ClientReflectionHelper.SetHeader(headers, key, Headers [key]);
                            break;
                        }
                    }
                }

                IAsyncResult async_result = new HttpWebAsyncResult(callback, state);
                asyncResult = (IAsyncResult)begin_get_response.Invoke(request,
                                                                      new object [2] {
                    new AsyncCallback(EndCallback), async_result
                });
                return(async_result);
            }
            catch (TargetInvocationException tie) {
                throw tie.InnerException;
            }
        }