this is not a question on how to abort thread pointhunters.
i am making a multi-threaded program which has a httpwebrequest in each one of the running threads and I figured that if I want to stop all of them what they are doing midway thru I must time them out.
is there a way to do that on a multi-threaded basis?
each thread looks like this:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
webRequest.Accept = "text/html";
webRequest.AllowAutoRedirect = false;
webRequest.Timeout = 1000 * 10;
webRequest.ServicePoint.Expect100Continue = false;
webRequest.ServicePoint.ConnectionLimit = 100;
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36";
webRequest.Proxy = null;
WebResponse resp;
string htmlCode = null;
try
{
resp = webRequest.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8);
htmlCode = sr.ReadToEnd();
sr.Close();
resp.Close();
}
catch (Exception)
Timing out events explicitly is not a good method. You may want to look into
Cancel Async Tasks after a Period of Time.
You can cancel an asynchronous operation after a period of time by
using the CancellationTokenSource.CancelAfter method if you don't want
to wait for the operation to finish. This method schedules the
cancellation of any associated tasks that aren’t complete within the
period of time that’s designated by the CancelAfter expression.
Sample code from MSDN :
// Declare a System.Threading.CancellationTokenSource.
CancellationTokenSource cts;
private async void startButton_Click(object sender, RoutedEventArgs e)
{
// Instantiate the CancellationTokenSource.
cts = new CancellationTokenSource();
resultsTextBox.Clear();
try
{
// ***Set up the CancellationTokenSource to cancel after 2.5 seconds. (You
// can adjust the time.)
cts.CancelAfter(2500);
await AccessTheWebAsync(cts.Token);
resultsTextBox.Text += "\r\nDownloads succeeded.\r\n";
}
catch (OperationCanceledException)
{
resultsTextBox.Text += "\r\nDownloads canceled.\r\n";
}
catch (Exception)
{
resultsTextBox.Text += "\r\nDownloads failed.\r\n";
}
cts = null;
}
// You can still include a Cancel button if you want to.
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
if (cts != null)
{
cts.Cancel();
}
}
async Task AccessTheWebAsync(CancellationToken ct)
{
// Declare an HttpClient object.
HttpClient client = new HttpClient();
// Make a list of web addresses.
List<string> urlList = SetUpURLList();
foreach (var url in urlList)
{
// GetAsync returns a Task<HttpResponseMessage>.
// Argument ct carries the message if the Cancel button is chosen.
// Note that the Cancel button cancels all remaining downloads.
HttpResponseMessage response = await client.GetAsync(url, ct);
// Retrieve the website contents from the HttpResponseMessage.
byte[] urlContents = await response.Content.ReadAsByteArrayAsync();
resultsTextBox.Text +=
String.Format("\r\nLength of the downloaded string: {0}.\r\n"
, urlContents.Length);
}
}
There is also Thread.Abort Method which terminates the thread.
EDIT : Canceling the Task - A better Explanation (source)
The Task class provides a way to cancel the started task based on the CancellationTokenSource class.
Steps to cancel a task:
The asynchronous method should except a parameter of type CancellationToken
Create an instance of CancellationTokenSource class like: var cts = new CancellationTokenSource();
Pass the CancellationToken from the instace to the asynchronous method, like: Task<string> t1 = GreetingAsync("Bulbul", cts.Token);
From the long running method, we have to call ThrowIfCancellationRequested() method of CancellationToken.
static string Greeting(string name, CancellationToken token)
{
Thread.Sleep(3000);
token. ThrowIfCancellationRequested();
return string.Format("Hello, {0}", name);
}
Catch the OperationCanceledException from where we are awiting for the Task.
We can cancel the operation by calling Cancel method of instance of CancellationTokenSource, OperationCanceledException will be thrown from the long running operation. We can set time to cancel the operation to the instanc also.
Further Details - MSDN Link.
static void Main(string[] args)
{
CallWithAsync();
Console.ReadKey();
}
async static void CallWithAsync()
{
try
{
CancellationTokenSource source = new CancellationTokenSource();
source.CancelAfter(TimeSpan.FromSeconds(1));
var t1 = await GreetingAsync("Bulbul", source.Token);
}
catch (OperationCanceledException ex)
{
Console.WriteLine(ex.Message);
}
}
static Task<string> GreetingAsync(string name, CancellationToken token)
{
return Task.Run<string>(() =>
{
return Greeting(name, token);
});
}
static string Greeting(string name, CancellationToken token)
{
Thread.Sleep(3000);
token.ThrowIfCancellationRequested();
return string.Format("Hello, {0}", name);
}
Related
I have a functionality of search users. I have provided a textview and on that textview changed method I'm firing a method to get data from web server. But I'm facing problem when user types letter, because all the api hits done in async task. Service should be hit after 100 milli-sec of wait, means if user types a letter "a" then doesn't type for 100 milli-sec then We have to hit the service. But if user types "a" then "b" then "c", so one service should be hit for "abc", not for all.
I followed the official link, but it doesn't help me
https://msdn.microsoft.com/en-us/library/jj155759.aspx
So basically here is my code
textview.TextChange+= (sender,e) =>{
CancellationTokenSource cts = new CancellationTokenSource();
await Task.Delay(500);
// here some where I have to pass cancel token
var lst = await APIClient.Instance.GetUserSearch("/user/get?searchTerm=" + newText, "application/json",cts);
if (lst != null && lst.Count > 0){
lstSearch.AddRange(lst);
}
}
Here is my method to GetUser
public async Task<JResponse> GetUserSearch<JResponse>(string uri, string contentType,CancellationToken cts)
{
try
{
Console.Error.WriteLine("{0}", RestServiceBaseAddress + uri);
string url = string.Format("{0}{1}", RestServiceBaseAddress, uri);
var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = contentType;
if (Utility.CurrentUser != null && !string.IsNullOrWhiteSpace(Utility.CurrentUser.AuthToken))
{
request.Headers.Add("api_key", Utility.CurrentUser.AuthToken);
}
request.Method = "POST";
var payload = body.ToString();
request.ContentLength = payload.Length;
byte[] byteArray = Encoding.UTF8.GetBytes(body.ToString());
request.ContentLength = byteArray.Length;
using (var stream = await request.GetRequestStreamAsync())
{
stream.Write(byteArray, 0, byteArray.Length);
stream.Close();
}
using (var webResponse = await request.GetResponseAsync())
{
var response = (HttpWebResponse)webResponse;
using (var reader1 = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine("Finished : {0}", uri);
var responseStr = reader1.ReadToEnd();
var responseObj = JsonConvert.DeserializeObject<JResponse>(
responseStr,
new JsonSerializerSettings()
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
});
return responseObj;
}
}
}
catch (System.Exception ex)
{
Utility.ExceptionHandler("APIClient", "ProcessRequestAsync", ex);
}
return default(JResponse);
}
In your example, you are creating a CancellationTokenSource - you need to hold a reference to it, so that the next time the handler is invoked, the previous search can be cancelled. Here is an example console app that you should be able to run, but the important piece is in the handler.
private CancellationTokenSource _cts;
private async void TextChangedHandler(string text) // async void only for event handlers
{
try
{
_cts?.Cancel(); // cancel previous search
}
catch (ObjectDisposedException) // in case previous search completed
{
}
using (_cts = new CancellationTokenSource())
{
try
{
await Task.Delay(TimeSpan.FromSeconds(1), _cts.Token); // buffer
var users = await _userService.SearchUsersAsync(text, _cts.Token);
Console.WriteLine($"Got users with IDs: {string.Join(", ", users)}");
}
catch (TaskCanceledException) // if the operation is cancelled, do nothing
{
}
}
}
Be sure to pass the CancellationToken into all of the async methods, including those that perform the web request, this way you signal the cancellation right down to the lowest level.
Try to use timer. First time then you change text - you create it. Then you change text after that - you restart timer. If you don't change text for 700 milliseconds - timer will fire PerformeSearch method. Use Timeout.Infinite for timer period parameter to prevent it from restarting.
textview.TextChange += (sender,e) =>
{
if (_fieldChangeTimer == null)
_fieldChangeTimer = new Timer(delegate
{
PerformeSearch();
}, null, 700, Timeout.Infinite);
else
{
_fieldChangeTimer.Change(700, Timeout.Infinite);
}
};
Instantiate the CancellationTokenSource.
cts = new CancellationTokenSource(); Example method
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
if (cts != null)
{
cts.Cancel();
}
}
I need to set a timeout of 10 seconds to all the Http web request in Wp8.1 app. I dont find a Timeout instead a ContinueTimeout property in the HttpWebRequest class.
A Quick search gave be few alternatives. Using a CancellationToken being one and the other one is using Task. Will some one give me pointers as to how to modify my current code.
This is how I'm creating a request
string uri = MyClass.HTTP_URI + "user/server-timestamps";
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = MyClass.HTTP_GET;
request.Accept = "application/json";
request.Headers[HTTP_AUTHENTICATION_TOKEN] = "token"
request.Headers[API_KEY] = API_KEY_VALUE;
This is how I'm sending the request
try
{
WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
HttpWebResponse response = (HttpWebResponse)responseObject;
statusCode = (int)response.StatusCode;
if (statusCode == 200)
{
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
received = await sr.ReadToEndAsync();
//Do stuff
}
}
You can use HttpClient class and CancellationTokenSource. Just modify it.
try
{
CancellationTokenSource cts = new CancellationTokenSource(2000);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new Windows.Web.Http.Headers.HttpMediaTypeWithQualityHeaderValue(""));
HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), new Uri("Url"));
HttpResponseMessage response = await client.SendRequestAsync(msg).AsTask(cts.Token);
}
catch(TaskCanceledException ex)
{
}
You can create an extension method than accepts CancellationToken and use it like this:
var request = (HttpWebRequest)WebRequest.Create(uri);
// ...
try
{
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
{
await request.GetResponseAsyncCancelable(cts.Token);
}
}
catch (OperationCanceledException)
{
// handle cancellation, if desired
}
// ...
public static class HttpWebRequestExt
{
public static async Task<HttpWebResponse> GetResponseAsyncCancelable(
this HttpWebRequest #this,
CancellationToken token)
{
using (token.Register(() => request.Abort(), useSynchronizationContext: false))
{
try
{
// BTW: any reason why not use request.GetResponseAsync,
// rather than FromAsync? It's there in WP 8.1:
// var response = await request.GetResponseAsync();
var response = (HttpWebResponse)await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
token.ThrowIfCancellationRequested();
return response;
}
catch (WebException ex)
{
// WebException caused by cancellation?
if (!token.IsCancellationRequested)
throw; // no, just re-throw
// yes, wrap it with OperationCanceledException
throw new OperationCanceledException(ex.Message, ex, token);
}
}
}
}
BTW, is there any reason you're not using GetResponseAsync, which is there in WP 8.1? See the code comments inline.
I have a question regarding strange behavior of a process that performs lots of async unsuccessful requests to a server.
The strange thing is that it's almost impossible to exit/stop/kill the process.
If you compile and run this program on a windows 8 machine the process hangs and I haven't found a solution how to kill it. Only reboot helps.
Could somebody explain the mentioned behavior please.
Thanks.
PS: This is what I get when use taskmanager to kill the process
internal class Program
{
private const int totalRequests = 2000;
private static void Main(string[] args)
{
var cancellationTockenSource = new CancellationTokenSource();
LoadTest(cancellationTockenSource);
Console.WriteLine("Press any key to break");
Console.ReadLine();
cancellationTockenSource.Cancel();
}
private static async Task LoadTest(CancellationTokenSource cancellationTockenSource)
{
Stopwatch stopWatch = Stopwatch.StartNew();
var requests = new List<Task>(totalRequests);
for (int i = 0; i < totalRequests; ++i)
{
requests.Add(Request(cancellationTockenSource.Token));
}
try
{
await Task.WhenAll(requests);
}
catch (Exception e)
{
Console.WriteLine(e);
}
stopWatch.Stop();
Console.WriteLine("{0} req/sec", stopWatch.Elapsed.TotalSeconds/totalRequests);
}
private static HttpRequestMessage CreateMessage()
{
var url = new Uri("http://ya.ru:8080/234234234234x");
var message = new HttpRequestMessage(HttpMethod.Get, url);
message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
return message;
}
protected static async Task<string> Request(CancellationToken token)
{
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.SendAsync(CreateMessage(), token);
string content = await response.Content.ReadAsStringAsync();
return content;
}
}
}
I have an component that is processing multiple web requests each in separate thread. Each WebRequest processing is synchronous.
public class WebRequestProcessor:System.ComponentModel.Component
{
List<Worker> tlist = new List<Worker>();
public void Start()
{
foreach(string url in urlList){
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start(url);
tlist.Add(workerThread);
}
}
}
public class Worker
{
// This method will be called when the thread is started.
public void DoWork(string url)
{
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
// process stream
}
}
Now I have to find optimal way how to cancel all requests.
One way is to convert each synchronous WebRequest into async and use WebRequest.Abort to cancel processing.
Another way is to release thread pointers and allow all threads to die using GC.
If you want to download 1000 files, starting 1000 threads at once is certainly not the best option. Not only it probably won't get you any speedup when compared with downloading just a few files at a time, it will also require at least 1 GB of virtual memory. Creating threads is expensive, try to avoid doing so in a loop.
What you should do instead is to use Parallel.ForEach() along with the asynchronous versions of the request and response operations. For example like this (WPF code):
private void Start_Click(object sender, RoutedEventArgs e)
{
m_tokenSource = new CancellationTokenSource();
var urls = …;
Task.Factory.StartNew(() => Start(urls, m_tokenSource.Token), m_tokenSource.Token);
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
m_tokenSource.Cancel();
}
void Start(IEnumerable<string> urlList, CancellationToken token)
{
Parallel.ForEach(urlList, new ParallelOptions { CancellationToken = token },
url => DownloadOne(url, token));
}
void DownloadOne(string url, CancellationToken token)
{
ReportStart(url);
try
{
var request = WebRequest.Create(url);
var asyncResult = request.BeginGetResponse(null, null);
WaitHandle.WaitAny(new[] { asyncResult.AsyncWaitHandle, token.WaitHandle });
if (token.IsCancellationRequested)
{
request.Abort();
return;
}
var response = request.EndGetResponse(asyncResult);
using (var stream = response.GetResponseStream())
{
byte[] bytes = new byte[4096];
while (true)
{
asyncResult = stream.BeginRead(bytes, 0, bytes.Length, null, null);
WaitHandle.WaitAny(new[] { asyncResult.AsyncWaitHandle,
token.WaitHandle });
if (token.IsCancellationRequested)
break;
var read = stream.EndRead(asyncResult);
if (read == 0)
break;
// do something with the downloaded bytes
}
}
response.Close();
}
finally
{
ReportFinish(url);
}
}
This way, when you cancel the operation, all downloads are canceled and no new ones are started. Also, you probably want to set MaxDegreeOfParallelism of ParallelOptions, so that you aren't doing too many downloads at once.
I'm not sure what do you want to do with the files you are downloading, so using StreamReader might be a better option.
I think the best solution is "Parallel Foreach Cancellation". Please check the following code.
To implement a cancellation, you first make CancellationTokenSource and pass it to Parallel.ForEach through option.
If you want to cancel, you can call CancellationTokenSource.Cancel()
After the cancelling, OperationCanceledException will be occurred, which you need to handle.
There is a good article about Parallel Programming related to my answer, which is Task Parallel Library By Sacha Barber on CodeProject.
CancellationTokenSource tokenSource = new CancellationTokenSource();
ParallelOptions options = new ParallelOptions()
{
CancellationToken = tokenSource.Token
};
List<string> urlList = null;
//parallel foreach cancellation
try
{
ParallelLoopResult result = Parallel.ForEach(urlList, options, (url) =>
{
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
workerObject.DoWork(url);
});
}
catch (OperationCanceledException ex)
{
Console.WriteLine("Operation Cancelled");
}
UPDATED
The following code is "Parallel Foreach Cancellation Sample Code".
class Program
{
static void Main(string[] args)
{
List<int> data = ParallelEnumerable.Range(1, 10000).ToList();
CancellationTokenSource tokenSource = new CancellationTokenSource();
Task cancelTask = Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
tokenSource.Cancel();
});
ParallelOptions options = new ParallelOptions()
{
CancellationToken = tokenSource.Token
};
//parallel foreach cancellation
try
{
Parallel.ForEach(data,options, (x, state) =>
{
Console.WriteLine(x);
Thread.Sleep(100);
});
}
catch (OperationCanceledException ex)
{
Console.WriteLine("Operation Cancelled");
}
Console.ReadLine();
}
}
How can I use HttpWebRequest (.NET, C#) asynchronously?
Use HttpWebRequest.BeginGetResponse()
HttpWebRequest webRequest;
void StartWebRequest()
{
webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}
void FinishWebRequest(IAsyncResult result)
{
webRequest.EndGetResponse(result);
}
The callback function is called when the asynchronous operation is complete. You need to at least call EndGetResponse() from this function.
By far the easiest way is by using TaskFactory.FromAsync from the TPL. It's literally a couple of lines of code when used in conjunction with the new async/await keywords:
var request = WebRequest.Create("http://www.stackoverflow.com");
var response = (HttpWebResponse) await Task.Factory
.FromAsync<WebResponse>(request.BeginGetResponse,
request.EndGetResponse,
null);
Debug.Assert(response.StatusCode == HttpStatusCode.OK);
If you can't use the C#5 compiler then the above can be accomplished using the Task.ContinueWith method:
Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse,
request.EndGetResponse,
null)
.ContinueWith(task =>
{
var response = (HttpWebResponse) task.Result;
Debug.Assert(response.StatusCode == HttpStatusCode.OK);
});
Considering the answer:
HttpWebRequest webRequest;
void StartWebRequest()
{
webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}
void FinishWebRequest(IAsyncResult result)
{
webRequest.EndGetResponse(result);
}
You could send the request pointer or any other object like this:
void StartWebRequest()
{
HttpWebRequest webRequest = ...;
webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), webRequest);
}
void FinishWebRequest(IAsyncResult result)
{
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
}
Greetings
Everyone so far has been wrong, because BeginGetResponse() does some work on the current thread. From the documentation:
The BeginGetResponse method requires some synchronous setup tasks to
complete (DNS resolution, proxy detection, and TCP socket connection,
for example) before this method becomes asynchronous. As a result,
this method should never be called on a user interface (UI) thread
because it might take considerable time (up to several minutes
depending on network settings) to complete the initial synchronous
setup tasks before an exception for an error is thrown or the method
succeeds.
So to do this right:
void DoWithResponse(HttpWebRequest request, Action<HttpWebResponse> responseAction)
{
Action wrapperAction = () =>
{
request.BeginGetResponse(new AsyncCallback((iar) =>
{
var response = (HttpWebResponse)((HttpWebRequest)iar.AsyncState).EndGetResponse(iar);
responseAction(response);
}), request);
};
wrapperAction.BeginInvoke(new AsyncCallback((iar) =>
{
var action = (Action)iar.AsyncState;
action.EndInvoke(iar);
}), wrapperAction);
}
You can then do what you need to with the response. For example:
HttpWebRequest request;
// init your request...then:
DoWithResponse(request, (response) => {
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.Write(body);
});
public static async Task<byte[]> GetBytesAsync(string url) {
var request = (HttpWebRequest)WebRequest.Create(url);
using (var response = await request.GetResponseAsync())
using (var content = new MemoryStream())
using (var responseStream = response.GetResponseStream()) {
await responseStream.CopyToAsync(content);
return content.ToArray();
}
}
public static async Task<string> GetStringAsync(string url) {
var bytes = await GetBytesAsync(url);
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
I ended up using BackgroundWorker, it is definitely asynchronous unlike some of the above solutions, it handles returning to the GUI thread for you, and it is very easy to understand.
It is also very easy to handle exceptions, as they end up in the RunWorkerCompleted method, but make sure you read this: Unhandled exceptions in BackgroundWorker
I used WebClient but obviously you could use HttpWebRequest.GetResponse if you wanted.
var worker = new BackgroundWorker();
worker.DoWork += (sender, args) => {
args.Result = new WebClient().DownloadString(settings.test_url);
};
worker.RunWorkerCompleted += (sender, e) => {
if (e.Error != null) {
connectivityLabel.Text = "Error: " + e.Error.Message;
} else {
connectivityLabel.Text = "Connectivity OK";
Log.d("result:" + e.Result);
}
};
connectivityLabel.Text = "Testing Connectivity";
worker.RunWorkerAsync();
.NET has changed since many of these answers were posted, and I'd like to provide a more up-to-date answer. Use an async method to start a Task that will run on a background thread:
private async Task<String> MakeRequestAsync(String url)
{
String responseText = await Task.Run(() =>
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
return new StreamReader(responseStream).ReadToEnd();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
return null;
});
return responseText;
}
To use the async method:
String response = await MakeRequestAsync("http://example.com/");
Update:
This solution does not work for UWP apps which use WebRequest.GetResponseAsync() instead of WebRequest.GetResponse(), and it does not call the Dispose() methods where appropriate. #dragansr has a good alternative solution that addresses these issues.
public void GetResponseAsync (HttpWebRequest request, Action<HttpWebResponse> gotResponse)
{
if (request != null) {
request.BeginGetRequestStream ((r) => {
try { // there's a try/catch here because execution path is different from invokation one, exception here may cause a crash
HttpWebResponse response = request.EndGetResponse (r);
if (gotResponse != null)
gotResponse (response);
} catch (Exception x) {
Console.WriteLine ("Unable to get response for '" + request.RequestUri + "' Err: " + x);
}
}, null);
}
}
Follow up to the #Isak 's answer, which is very good. Nonetheless it's biggest flaw is that it will only call the responseAction if the response has status 200-299. The best way to fix this is:
private void DoWithResponseAsync(HttpWebRequest request, Action<HttpWebResponse> responseAction)
{
Action wrapperAction = () =>
{
request.BeginGetResponse(new AsyncCallback((iar) =>
{
HttpWebResponse response;
try
{
response = (HttpWebResponse)((HttpWebRequest)iar.AsyncState).EndGetResponse(iar);
}
catch (WebException ex)
{
// It needs to be done like this in order to read responses with error status:
response = ex.Response as HttpWebResponse;
}
responseAction(response);
}), request);
};
wrapperAction.BeginInvoke(new AsyncCallback((iar) =>
{
var action = (Action)iar.AsyncState;
action.EndInvoke(iar);
}), wrapperAction);
}
And then as #Isak follows:
HttpWebRequest request;
// init your request...then:
DoWithResponse(request, (response) => {
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.Write(body);
});
I've been using this for async UWR, hopefully it helps someone
string uri = "http://some.place.online";
using (UnityWebRequest uwr = UnityWebRequest.Get(uri))
{
var asyncOp = uwr.SendWebRequest();
while (asyncOp.isDone == false) await Task.Delay(1000 / 30); // 30 hertz
if(uwr.result == UnityWebRequest.Result.Success) return uwr.downloadHandler.text;
Debug.LogError(uwr.error);
}