2022年2月26日 星期六

[研究][ASP.NET WebForm C#]HttpClient 呼叫 Web API、REST/RESTful API 測試(一)

[研究][ASP.NET WebForm C#]HttpClient 呼叫 Web API、REST/RESTful API 測試(一)

2022-02-26

續這篇

[研究][ASP.NET WebForm C#]WebClient 呼叫 Web API、REST/RESTful API 測試
https://shaurong.blogspot.com/2022/02/aspnet-webform-cwebclient-web.html

Visual Studio2022 v17.1.0 + ASP.NET + C# + Web Application + WebForm

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
    <asp:Label ID="Label_MSG1" runat="server" Text="Label"></asp:Label>
</body>
</html>

Default.aspx.cs


using System;
using System.Collections.Generic;
using System.Net.Http;

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            // WebRequest、WebClient 和 >servicepoint 已淘汰
            // https://docs.microsoft.com/zh-tw/dotnet/core/compatibility/networking/6.0/webrequest-deprecated
            // 請改用 System.Net.Http.HttpClient 類別。

            MultipartFormDataContent content = new MultipartFormDataContent();
            using (HttpClient client = new HttpClient())
            {
                //client.DefaultRequestHeaders.Add("");
                string account = "帳號";
                string password = "密碼";
                var values = new[]
                {
                    new KeyValuePair<string, string>("account", account),
                    new KeyValuePair<string, string>("pass", password)
                };
                using (FormUrlEncodedContent multipartFormDataContent = new FormUrlEncodedContent(values))
                {
                    var requestUri = "https://網址/rest/verify";
                    var json = client.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
                    Label_MSG1.Text = json;
					
		    //VerifyResult vResult = JsonConvert.DeserializeObject<VerifyResult>(json);
					
		    //JavaScriptSerializer js = new JavaScriptSerializer();
                    //VerifyResult info = js.Deserialize<VerifyResult>(html);
                }
            }
        }
    }
}

後來發現

HttpClient 類別
https://docs.microsoft.com/zh-tw/dotnet/api/system.net.http.httpclient?view=net-6.0&WT.mc_id=DOP-MVP-37580&viewFallbackFrom=netframework-4
HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
HttpClient 應該只建立一份並重複利用。為每次請求建立新的 HttpClient,重度使用下可能用光 Socket Port 導致 SocketException。以下是正確的寫法:

(完)

相關

[研究][ASP.NET WebForm C#]HttpClient 呼叫 Web API、REST/RESTful API 測試(二)
https://shaurong.blogspot.com/2022/02/aspnet-webform-chttpclient-web_26.html

[研究][ASP.NET WebForm C#]HttpClient 呼叫 Web API、REST/RESTful API 測試(一)
http://shaurong.blogspot.com/2022/02/aspnet-webform-chttpclient-web.html

[研究][ASP.NET WebForm C#]WebClient 呼叫 Web API、REST/RESTful API 測試https://shaurong.blogspot.com/2022/02/aspnet-webform-cwebclient-web.html


沒有留言:

張貼留言