本文整理汇总了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;
}
示例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;
}
示例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;
}
}
示例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;
}
示例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);
}
示例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;
}
示例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
}
示例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);
}
示例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);
}
示例10: GetResponseAsync
public static K<WebResponse> GetResponseAsync(this WebRequest request)
{
return respond => request.BeginGetResponse(result =>
{
var response = request.EndGetResponse(result);
respond(response);
}, null);
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
}
示例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;
}