簡體   English   中英

具有PostAsync代碼優化的HttpClient

[英]HttpClient with PostAsync code optimisation

我正在使用下面的代碼,代碼工作正常。 我是HTTPClient的新手,所以我不確定此代碼是否經過適當優化,或者最好的編碼方式是什么。 我發現了這個示例,盡管它涉及的是並行編程,但該示例仍在討論死鎖,但是我只是想確保是否可以針對性能或錯誤來改進/優化此代碼。

我使用特定的關鍵參數連接到網站並獲取返回的json數據。 ii。采取進一步行動的過程。

protected void btnClient_Click(object sender, EventArgs e)
    {
        using (var client = new HttpClient())
        {

            client.BaseAddress = new Uri("https://secure.telr.com/");
            client.DefaultRequestHeaders.ExpectContinue = false;
            var result = client.PostAsync("gateway/order.json",
                new FormUrlEncodedContent(new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("ivp_method", "create"),
                new KeyValuePair<string, string>("ivp_store", "12345"),
                new KeyValuePair<string, string>("ivp_authkey", "xxx-xxxxxx"),
                new KeyValuePair<string, string>("ivp_cart", "123452"),
                new KeyValuePair<string, string>("ivp_desc", "Descripion"),
                new KeyValuePair<string, string>("ivp_test", "1"),
                new KeyValuePair<string, string>("ivp_amount", "10.00"),
                new KeyValuePair<string, string>("ivp_currency", "UAD"),
                new KeyValuePair<string, string>("return_auth", "http://localhost:1044/Test2.aspx"),
                new KeyValuePair<string, string>("return_can", "http://localhost:1044/Test2.aspx"),
                new KeyValuePair<string, string>("return_decl", "http://localhost:1044/Test2.aspx"),
                new KeyValuePair<string, string>("ivp_framed", "1"),
            })).Result;


            var jsonData = (JObject)JsonConvert.DeserializeObject(result.Content.ReadAsStringAsync().Result);

            dynamic jObj = JsonConvert.DeserializeObject(result.Content.ReadAsStringAsync().Result);

            string url = jsonData["order"]["url"].ToString();
            Response.Write("<br>url" + url + "<br>");

            ltrTelr.Text = "<iframe id= 'telr' src='" + url + "' ></iframe>";

        }
    }

當您不確定API是否/何時返回數據時,應避免嘗試訪問Task.Result。 那會阻塞您的線程,因為它正在嘗試評估結果。

相反,在調用異步方法時,應使用“ await”關鍵字,以便釋放線程,如下所示:

protected async Task btnClient_Click(object sender, EventArgs e)
{
    using (var client = new HttpClient())
    {

        client.BaseAddress = new Uri("https://secure.telr.com/");
        client.DefaultRequestHeaders.ExpectContinue = false;
        var result = await client.PostAsync("gateway/order.json",
            new FormUrlEncodedContent(new List<KeyValuePair<string, string>>()
        {
            new KeyValuePair<string, string>("ivp_method", "create"),
            new KeyValuePair<string, string>("ivp_store", "12345"),
            new KeyValuePair<string, string>("ivp_authkey", "xxx-xxxxxx"),
            new KeyValuePair<string, string>("ivp_cart", "123452"),
            new KeyValuePair<string, string>("ivp_desc", "Descripion"),
            new KeyValuePair<string, string>("ivp_test", "1"),
            new KeyValuePair<string, string>("ivp_amount", "10.00"),
            new KeyValuePair<string, string>("ivp_currency", "UAD"),
            new KeyValuePair<string, string>("return_auth", "http://localhost:1044/Test2.aspx"),
            new KeyValuePair<string, string>("return_can", "http://localhost:1044/Test2.aspx"),
            new KeyValuePair<string, string>("return_decl", "http://localhost:1044/Test2.aspx"),
            new KeyValuePair<string, string>("ivp_framed", "1"),
        }));

        var rawData = await result.Content.ReadAsStringAsync();

        var jsonData = (JObject)JsonConvert.DeserializeObject(rawData);
        dynamic jObj = JsonConvert.DeserializeObject(rawData);

        string url = jsonData["order"]["url"].ToString();
        Response.Write("<br>url" + url + "<br>");

        ltrTelr.Text = "<iframe id= 'telr' src='" + url + "' ></iframe>";

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM