http客户端在c#中发布async

时间:2018-03-08 13:58:02

标签: asynchronous f# httpclient c#-to-f#

我已经看过这个example在F#中发帖请求,但我想知道如何使用异步发布请求来做同样的事情

type Authentication = 
    new() = {}
    member this.RequestToken() =
        use client = new HttpClient()
        client.PostAsync "http://example.com"

        printfn "requestToken"

修改

根据评论中的请求提供可比较的C#代码。

var body = "some data";

using (var client = new HttpClient(new HttpClientHandler { UseProxy = false })) {

    var response = await client.PostAsync("https://example.com", new StringContent(body, Encoding.UTF8, "application/xml"));
    var content = await response.Content.ReadAsStringAsync();
}

1 个答案:

答案 0 :(得分:2)

对于F#异步代码,您需要使用async计算表达式和Async.AwaitTask将C#的Task<_>类型转换为F#&#39; s {{1} }。我建议你查阅一些关于F#异步工作流程的文档。

Async<_>