C# 開発

【C# GroupSession】自分の個人情報を取得 (/api/user/whoami.do?)

準備

(なし)

デザイン

  • フォーム (Form1) にボタン (button1) を配置します。
  • フォーム (Form1) にテキストボックス (textBox1) を配置します。

サンプルコード (C#)

using System.Net;
using System.Xml.Linq;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string LoginUSR = "tanaka";
            string LoginPWD = "9999";

            string GSAddress = "http://192.168.2.122:8080/gsession";
            string GSAPI = "/api/user/whoami.do?";

            textBox1.Text = $@"{GSAddress}{GSAPI}";

            var clientInfo = new HttpClientHandler
            {
                Credentials = new NetworkCredential(LoginUSR, LoginPWD)
            };

            var client = new HttpClient(clientInfo);
            var response = client.GetAsync(textBox1.Text).Result;
            var result = response.Content.ReadAsStringAsync().Result;

            textBox2.Text = result;
        }
    }
}

解説

GroupSession の /api/user/whoami.do? をコールすることで、ログインユーザーの情報を取得します。

結果

動作確認環境

Visual Studio 2022 Professional (.NET8 C#12)

ログ

初版:2024.02.26 Visual Studio 2022 Professional (C#12)

-C# 開発