当前位置: 首页>>代码示例>>C#>>正文


C# this.BeginGetResponse方法代码示例

本文整理汇总了C#中this.BeginGetResponse方法的典型用法代码示例。如果您正苦于以下问题:C# this.BeginGetResponse方法的具体用法?C# this.BeginGetResponse怎么用?C# this.BeginGetResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在this的用法示例。


在下文中一共展示了this.BeginGetResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetResponseNoEx

        public static WebResponse GetResponseNoEx(this WebRequest req)
        {
            WebResponse result = null;
            ManualResetEvent responseReady = new ManualResetEvent(false);

            AsyncCallback callback = new AsyncCallback(ar =>
                {
                        //var request = (WebRequest)ar.AsyncState;
                        result = req.EndGetResponseNoEx(ar);
                        responseReady.Set();
                });

            var async = req.BeginGetResponse(callback, null);

            if (!async.IsCompleted)
            {
                //async.AsyncWaitHandle.WaitOne();
                // Not having thread affinity seems to work better with ManualResetEvent
                // Using AsyncWaitHandle.WaitOne() gave unpredictable results (in the
                // unit tests), when EndGetResponse would return null without any error
                // thrown
                responseReady.WaitOne();
                //async.AsyncWaitHandle.WaitOne();
            }

            return result;
        }
开发者ID:peterswallow,项目名称:fhir-net-api,代码行数:27,代码来源:WebRequestExtensions.cs

示例2: GetResponseAsync

        public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
        {
            var tcs = new TaskCompletionSource<HttpWebResponse>();

            try
            {
                request.BeginGetResponse(iar =>
                {
                    try
                    {
                        var response = (HttpWebResponse)request.EndGetResponse(iar);
                        tcs.SetResult(response);
                    }
                    catch (Exception exc)
                    {
                        tcs.SetException(exc);
                    }
                }, null);
            }
            catch (Exception exc)
            {
                tcs.SetException(exc);
            }

            return tcs.Task;
        }
开发者ID:JanJoris,项目名称:Fuel,代码行数:26,代码来源:HttpWebRequestExtensions.cs

示例3: ToHttpCall

        public static HttpResponse ToHttpCall(this WebRequest request)
        {
            var reset = new ManualResetEvent(false);
            IAsyncResult result = null;

            var thread = new Thread(() =>
            {
                result = request.BeginGetResponse(r => { }, null);
                reset.Set();
            });
            thread.Start();
            thread.Join();
            reset.WaitOne();

            try
            {
                var response = request.EndGetResponse(result).As<HttpWebResponse>();
                return new HttpResponse(response);
            }
            catch (WebException e)
            {
                if (e.Response == null)
                {
                    throw;
                }

                var errorResponse = new HttpResponse(e.Response.As<HttpWebResponse>());

                return errorResponse;
            }
        }
开发者ID:joemcbride,项目名称:fubumvc,代码行数:31,代码来源:WebRequestResponseExtensions.cs

示例4: GetResponseAsync

		public static Task<WebResponse> GetResponseAsync(this HttpWebRequest request)
		{
			var taskComplete = new TaskCompletionSource<WebResponse>();

			request.AllowReadStreamBuffering = true;
			request.AllowWriteStreamBuffering = true;

			request.BeginGetResponse(asyncResponse =>
			{
				try
				{
					var responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
					var someResponse = responseRequest.EndGetResponse(asyncResponse);

					taskComplete.TrySetResult(someResponse);
				}
				catch (WebException webExc)
				{
					var failedResponse = webExc.Response;
					taskComplete.TrySetResult(failedResponse);
				}
			}, request);

			return taskComplete.Task;
		}
开发者ID:selaromdotnet,项目名称:Coding4FunToolkit,代码行数:25,代码来源:HttpWebRequestExtensions.cs

示例5: DownloadStringAsync

        public static void DownloadStringAsync(this WebRequest request, Encoding encoding, Action<string> callback)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            if (encoding == null)
                throw new ArgumentNullException("encoding");

            if (callback == null)
                throw new ArgumentNullException("callback");

            request.BeginGetResponse((IAsyncResult result) =>
            {
                try
                {
                    var response = request.EndGetResponse(result);
                    using (var reader = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        callback(reader.ReadToEnd());
                    }
                }
                catch (WebException e)
                {
                    // Don't perform a callback, as this error is mostly due to
                    // there being no internet connection available.
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }, request);
        }
开发者ID:jesperll,项目名称:DMI-Byvejr,代码行数:29,代码来源:WebRequestEx.cs

示例6: GetResponseStreamAsync

        public static Task<HttpWebResponse> GetResponseStreamAsync(this HttpWebRequest context, object state)
        {
            // this will be our sentry that will know when our async operation is completed
            var tcs = new TaskCompletionSource<HttpWebResponse>();

            try
            {
                context.BeginGetResponse((iar) =>
                {
                    try
                    {
                        var result = context.EndGetResponse(iar as IAsyncResult);
                        tcs.TrySetResult(result as HttpWebResponse);
                    }
                    catch (OperationCanceledException ex)
                    {
                        // if the inner operation was canceled, this task is cancelled too
                        tcs.TrySetCanceled();
                    }
                    catch (Exception ex)
                    {
                        // general exception has been set
                        tcs.TrySetException(ex);
                    }
                }, state);
            }
            catch
            {
                tcs.TrySetResult(default(HttpWebResponse));
                // propagate exceptions to the outside
                throw;
            }

            return tcs.Task;
        }
开发者ID:sridhar19091986,项目名称:sharpmapx,代码行数:35,代码来源:HttpExtensions.cs

示例7: GetResponse

        /// <include file='../_Doc/System.xml' path='doc/members/member[@name="M:System.Net.WebRequest.GetResponse"]/*' />
        /// <param name="request">HTTP web request object on which to get response.</param>
        public static WebResponse GetResponse(this WebRequest request)
        {
#if DOTNET
            return request.GetResponse();
#else
            return request.EndGetResponse(request.BeginGetResponse(null, null));
#endif
        }
开发者ID:BeeINSIGHT,项目名称:shim,代码行数:10,代码来源:WebRequestExtensions.cs

示例8: GetResponse

        public static WebResponse GetResponse(this HttpWebRequest request)
        {
            var autoResetEvent = new AutoResetEvent(false);
            IAsyncResult asyncResult = request.BeginGetResponse(r => autoResetEvent.Set(), null);
            autoResetEvent.WaitOne();

            return request.EndGetResponse(asyncResult);
        }
开发者ID:pazof,项目名称:MonoCross,代码行数:8,代码来源:HttpExtensions.cs

示例9: GetResponse

 public static WebResponse GetResponse(this WebRequest request)
 {
     AutoResetEvent autoResetEvent = new AutoResetEvent(false);
     IAsyncResult asyncResult = request.BeginGetResponse(r => autoResetEvent.Set(), null);
     // Wait until the call is finished
     autoResetEvent.WaitOne();
     return request.EndGetResponse(asyncResult);
 }
开发者ID:ARChess,项目名称:ARChess,代码行数:8,代码来源:HttpWebExtensions.cs

示例10: GetResponseAsync

 public static K<WebResponse> GetResponseAsync(this WebRequest request)
 {
     return respond => request.BeginGetResponse(result =>
         {
             var response = request.EndGetResponse(result);
             respond(response);
         }, null);
 }
开发者ID:KevM,项目名称:Magnum,代码行数:8,代码来源:ExtensionsForWebStuff.cs

示例11: GetResponseContent

 public static void GetResponseContent(this HttpWebRequest request, Action<string> result)
 {
     request.BeginGetResponse((iar) =>
     {
         WebResponse response = request.EndGetResponse(iar);
         using (var sr = new StreamReader(response.GetResponseStream()))
         {
             result(sr.ReadToEnd());
         }
     }, request);
 }
开发者ID:halllo,项目名称:SilverlightRun,代码行数:11,代码来源:WebHelper.cs

示例12: GetResponse

 public static WebResponse GetResponse(this WebRequest request)
 {
     ManualResetEvent evt = new ManualResetEvent(false);
     WebResponse response = null;
     request.BeginGetResponse((IAsyncResult ar) => {
         response = request.EndGetResponse(ar);
         evt.Set();
     }, null);
     evt.WaitOne();
     return response as WebResponse;
 }
开发者ID:abethcrane,项目名称:ms-band-smartthings-tile,代码行数:11,代码来源:Helpers.cs

示例13: ExecuteGetAsync

        public static IAsyncResult ExecuteGetAsync(this WebRequest request,
                                                   Action<WebResponseEventArgs>
                                                       callback)
        {
            Console.WriteLine("GET: {0}", request.RequestUri);

            ClearLastResponse();

            return
                request.BeginGetResponse(
                    result => RaiseWebResponse(request, result, callback), null);
        }
开发者ID:remiolivier,项目名称:Openturf,代码行数:12,代码来源:NUrl.Get.cs

示例14: ToHttpCall

        public static HttpResponse ToHttpCall(this WebRequest request)
        {
            var result = request.BeginGetResponse(r => { }, null);
            try
            {
                var response = request.EndGetResponse(result).As<HttpWebResponse>();
                return new HttpResponse(response);
            }
            catch (WebException e)
            {
                var errorResponse = new HttpResponse(e.Response.As<HttpWebResponse>());
                Debug.WriteLine(errorResponse.ToString());

                return errorResponse;
            }
        }
开发者ID:jemacom,项目名称:fubumvc,代码行数:16,代码来源:WebRequestResponseExtensions.cs

示例15: GetResponseAsync

 public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
 {
     var taskComplete = new TaskCompletionSource<HttpWebResponse>();
     request.BeginGetResponse(asyncResponse =>
     {
         try
         {
             HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
             HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
             taskComplete.TrySetResult(someResponse);
         } catch(WebException webExc) {
             HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
             taskComplete.TrySetResult(failedResponse);
         }
     }, request);
     return taskComplete.Task;
 }
开发者ID:NewETown,项目名称:Re.WP8,代码行数:17,代码来源:HttpExtensions.cs


注:本文中的this.BeginGetResponse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。