F
F
Festelo2016-12-13 15:46:59
.NET
Festelo, 2016-12-13 15:46:59

Why is GetResponseAsync terminating the process?

There is a code:

string html = await GetDataAsync(url, cookie);
Console.WriteLine(html);

async Task<string> GetDataAsync(string url, string cookie) {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "GET";
            webRequest.Headers["UserAgent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(new Uri(url), new Cookie("express.sid", cookie));

            var response = await webRequest.GetResponseAsync();
            return new StreamReader(
                        response.GetResponseStream(), Encoding.UTF8).ReadToEnd();

And after the line ...
var response = await webRequest.GetResponseAsync();
... the program just exits without returning anything. Why?
.Net Core 1.0.1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Festelo, 2016-12-13
@Festelo

Replaced
on the

var response = webRequest.GetResponseAsync();
response.Wait();
response.Result();

And everything worked.

V
Vyacheslav Zolotov, 2016-12-13
@SZolotov

and if without cookies and user agent Yandex request what will happen?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question