我在下面写了两个方法
http://URLXXX.com/p/9998812/update_availability
POST /p/9998812/update_availability HTTP/1.1
Host: URLXXX.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://URLXXX.com/p/9998812/s/fwkA-irHT-2kMfS
Cookie:
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 138
utf8=%E2%9C%93&_method=put&authenticity_token=xMiaIdT%2Fnw%2FPbsYq%2BmVaLFnH362HIvIdXQQX3D%2F4uEo%3D&product%5Bstate%5D=active&commit=Save
HTTP/1.1 302 Found
Server: nginx/1.8.0
Date: Mon, 20 Jul 2015 06:00:16 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 93
Connection: keep-alive
Status: 302 Found
Location: http://URLXXX.com/p/9998812
Set-Cookie: makara-force-master=master; expires=Mon, 20-Jul-2015 06:00:21 GMT
Set-Cookie: csrf-param=authenticity_token; path=/
Set-Cookie: _ssn=c8a813425bc34cd850277f5745ff957e; domain=.URLXXX.com; path=/; expires=Mon, 20-Jul-2015 06:30:16 GMT; HttpOnly
X-UA-Compatible: IE=Edge,chrome=1
Cache-Control: no-cache
X-Request-Id: 3bcc2b5f06cdd5215a613e01726559d9
X-Runtime: 0.160565
X-Served-By: app102.c1.prod
----------------------------------------------------------
http://URLXXX.com/p/9998812
GET /p/9998812 HTTP/1.1
Host: URLXXX.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://URLXXX.com/p/9998812/s/fwkA-irHT-2kMfS
Cookie:
Connection: keep-alive
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Mon, 20 Jul 2015 06:00:16 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 127
Connection: keep-alive
Status: 301 Moved Permanently
Vary: User-Agent
Location: http://URLXXX.com/p/9998812/productdetail
X-UA-Compatible: IE=Edge,chrome=1
Cache-Control: no-cache
Set-Cookie: _ssn=c8a813425bc34cd850277f5745ff957e; domain=.URLXXX.com; path=/; expires=Mon, 20-Jul-2015 06:30:16 GMT; HttpOnly
X-Request-Id: 8b68dac5cb24355d19aa46c9ac22df61
X-Runtime: 0.020239
X-Served-By: app103.c1.prod
----------------------------------------------------------
http://URLXXX.com/p/9998812/productdetail
GET /p/9998812/productdetail HTTP/1.1
Host: URLXXX.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://URLXXX.com/p/9998812/s/fwkA-irHT-2kMfS
Cookie:
Connection: keep-alive
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Mon, 20 Jul 2015 06:00:17 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
Vary: User-Agent
X-UA-Compatible: IE=Edge,chrome=1
Etag: W/"2e41a435d3ea497f97654949f587fb46"
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: csrf-param=authenticity_token; path=/
Set-Cookie: _ssn=c8a813425bc34cd850277f5745ff957e; domain=.URLXXX.com; path=/; expires=Mon, 20-Jul-2015 06:30:17 GMT; HttpOnly
X-Request-Id: 7334d318bb69997260fd1b24f2d290de
X-Runtime: 0.369774
X-Served-By: app101.c1.prod
Content-Encoding: gzip
----------------------------------------------------------
我可以像下面的
一样调用另一个异步方法中的两个方法 private Task<string> GetStringTask(string url)
{
var client = new WebClient();
var task = client.DownloadDataTaskAsync(new Uri(url));
var task2 = task.ContinueWith<string>(task1 =>
{
var str = Encoding.Default.GetString(task1.Result);
Thread.Sleep(5000);
return str;
});
return task2;
}
private async Task<string> GetStringAsyc(string url)
{
var client = new WebClient();
var htmlByte = await client.DownloadDataTaskAsync(new Uri(url));
var task2 = await Task.Factory.StartNew(() =>
{
var str = Encoding.Default.GetString(htmlByte);
Thread.Sleep(2000);
return str;
});
return task2;
}
两种方法都返回相同的结果。任何人都可以解释我这两种方法之间的区别。
答案 0 :(得分:1)
async
修饰符只是确定您可以在函数体中使用await
关键字。
来自this回答:
它的引入主要是为了避免向后兼容性问题。如果 必须由编译器推断出方法的异步性(即 将通过检测等待关键字),然后有 突然处理现有代码的微妙场景 不同的是,特别是当你有标识符(变量或功能) 叫做等待的名字。