// NOTE: the supplied callback must be called only once the request is complete
        //	 i.e. it's not called after the policy is downloaded
        //	 i.e. it's not called after each redirection we need to follow
        public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
        {
            // Console.WriteLine ("{0} {1} {2}", GetType (), method, uri);
            // we're not allowed to reuse an aborted request
            if (IsAborted)
            {
                throw new WebException("Aborted", WebExceptionStatus.RequestCanceled);
            }

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

            // we cannot issue 2 requests from the same instance
            if (async_result != null)
            {
                throw new InvalidOperationException();
            }

            // this is the "global/total" IAsyncResult, it's also the public one
            async_result = new HttpWebAsyncResult(callback, state);

            GetResponse(this.Method, uri, true);
            return(async_result);
        }
        public override WebResponse EndGetResponse(IAsyncResult ar)
        {
            HttpWebAsyncResult async_result = ar as HttpWebAsyncResult;

            if (async_result == null)
            {
                throw new ArgumentException("asyncResult");
            }

            try {
                if (!async_result.IsCompleted)
                {
                    async_result.AsyncWaitHandle.WaitOne();
                }

                if (async_result.HasException)
                {
                    throw async_result.Exception;
                }

                try {
                    return(new ClientHttpWebResponse(this, response));
                }
                catch (TargetInvocationException tie) {
                    throw tie.InnerException;
                }
            }
            finally {
                async_result.Dispose();
            }
        }
示例#3
0
        public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
        {
            // we're not allowed to reuse an aborted request
            if (aborted)
            {
                throw new WebException("Aborted", WebExceptionStatus.RequestCanceled);
            }

            async_result = new HttpWebAsyncResult(callback, state);

            dispatcher.BeginInvoke(new Action(InitializeNativeRequestSafe), null);

            return(async_result);
        }
示例#4
0
		public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
		{
			WebClient.CallbackData callback_data = (state as WebClient.CallbackData);
			if (callback_data != null) {
				long length = callback_data.data.Length;
				// if Content-Length has been set previously and does not match the data length...
				if ((ContentLength != -1) && (ContentLength != length))
					throw new ProtocolViolationException ();
				ContentLength = length;
			}

			HttpWebAsyncResult result = new HttpWebAsyncResult (callback, state);
			result.SetComplete ();
			return result;
		}
        void EndCallback(IAsyncResult result)
        {
            HttpWebAsyncResult async_result = (result.AsyncState as HttpWebAsyncResult);

            try {
                response = end_get_response.Invoke(request, new object[] { asyncResult });
            }
            catch (TargetInvocationException tie) {
                async_result.Response  = new NotFoundWebResponse();
                async_result.Exception = tie.InnerException;
            }
            finally {
                async_result.SetComplete();
            }
        }
示例#6
0
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
        {
            WebClient.CallbackData callback_data = (state as WebClient.CallbackData);
            if (callback_data != null)
            {
                long length = callback_data.data.Length;
                // if Content-Length has been set previously and does not match the data length...
                if ((ContentLength != -1) && (ContentLength != length))
                {
                    throw new ProtocolViolationException();
                }
                ContentLength = length;
            }

            HttpWebAsyncResult result = new HttpWebAsyncResult(callback, state);

            result.SetComplete();
            return(result);
        }
示例#7
0
		// NOTE: the supplied callback must be called only once the request is complete
		//	 i.e. it's not called after the policy is downloaded
		//	 i.e. it's not called after each redirection we need to follow
		public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
		{
			// Console.WriteLine ("{0} {1} {2}", GetType (), method, uri);
			// we're not allowed to reuse an aborted request
			if (IsAborted)
				throw new WebException ("Aborted", WebExceptionStatus.RequestCanceled);

			// 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 ();

			// we cannot issue 2 requests from the same instance
			if (async_result != null)
				throw new InvalidOperationException ();

			// this is the "global/total" IAsyncResult, it's also the public one
			async_result = new HttpWebAsyncResult (callback, state);

			GetResponse (this.Method, uri, true);
			return async_result;
		}
		public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
		{
			// we're not allowed to reuse an aborted request
			if (aborted)
				throw new WebException ("Aborted", WebExceptionStatus.RequestCanceled);

			async_result = new HttpWebAsyncResult (callback, state);

			dispatcher.BeginInvoke (new Action (InitializeNativeRequestSafe), null);

			return async_result;
		}
		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) {
					string [] keys = Headers.AllKeys;
					foreach (string key in keys) {
						// we cannot set "Content-Type" using the headers
						if (String.Compare (key, "content-type", StringComparison.OrdinalIgnoreCase) != 0)
							ClientReflectionHelper.SetHeader (headers, key, Headers [key]);
					}
				}

				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;
			}
		}
示例#10
0
		public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
		{
			HttpWebAsyncResult result = new HttpWebAsyncResult (callback, state);
			result.SetComplete ();
			return result;
		}
示例#11
0
        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;
            }
        }
示例#12
0
		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;
			}
		}