2022年2月26日 星期六

[研究][ASP.NET WebForm C#]RestSharp 106.15.0 呼叫 Web API、REST/RESTful API 測試

[研究][ASP.NET WebForm C#]RestSharp v106.x 呼叫 Web API、REST/RESTful API 測試

2022-02-26

RestSharp is REST API client library for .NET

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

請用 NuGet 安裝 RestSharp 106 套件 (v107 語法大改)

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="RestSharp106Test.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>
        </div>
    </form>
    <asp:Label ID="Label1" runat="server"></asp:Label>
</body>
</html>


Default.aspx.cs


using RestSharp;
using System;
using System.Collections.Generic;

namespace RestSharp106Test
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = "帳號";
            string password = "密碼";
            VerifyResult content = Verify(username, password);
            Label1.Text = content.message;  // Return Code
            string contactName = content.data[0].contact_name;
} public static VerifyResult Verify(string account, string password) { var client = new RestClient("https://網址/rest/verify"); var request = new RestRequest(Method.POST); RestResponse<VerifyResult> response2; request.AddParameter("account", account); request.AddParameter("pass", password); try { //var aa = client.Execute(request); response2 = (RestSharp.RestResponse<VerifyResult>)client.Execute<VerifyResult>(request); } catch (Exception) { throw; } return response2.Data; } public class VerifyResult { public string message { get; set; } public string describe { get; set; } public List<VerifyData> data { get; set; } } public class VerifyData { public string contact_account { get; set; } public string contact_name { get; set; } public string contact_cellphone { get; set; } public string contact_mail { get; set; } public string contact_dept { get; set; } public string contact_tel { get; set; } public string contact_extend { get; set; } public string status { get; set; } public List<string> contact_category { get; set; } }
} }


(完)

相關

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

RestSharp Next (v107) | RestSharp
https://restsharp.dev/v107/#restsharp-v107

NuGet Gallery | RestSharp
https://www.nuget.org/packages/RestSharp/

RestSharp - Simple .NET REST Client - GitHub
https://github.com/restsharp/RestSharp

[研究][ASP.NET WebForm C#]Newtonsoft.Json 13.0.1 序列化、反序列化測試https://shaurong.blogspot.com/2022/02/aspnet-webform-cnewtonsoftjson-1301.html

[研究][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


沒有留言:

張貼留言