簡體   English   中英

C#.Net Framework WPF:如何進行 POST 調用並保存登錄 Baerer 密鑰?

[英]C# .Net Framework WPF: How can I do POST call and save login Baerer key?

我在我的 C# WPF 項目中使用了以下 POST 請求: https://stackoverflow.com/a/8091963/17282.3 所以我正在獲取用戶登錄 Bearer id,我想保存它(我使用 localstorage 來保存它,就像 reactjs 中的 redux 一樣)。 相同的頁面需要進行身份驗證。 我沒有找到一個好的解決方案。 這是我的代碼。

            using (var wb = new WebClient())
            {
                string Useremail = Login_email.Text;
                string Userpassword = Pass_password.Text;

                var data = new NameValueCollection();
                data["application_id"] = "22233sasdsa1123asdasxzczx";
                data["email"] = Useremail;
                data["password"] = Userpassword;

                var response = wb.UploadValues("https://example.com/user/api/logins?", "POST", data);
                string responseInString = Encoding.UTF8.GetString(response);
                test.Content = responseInString;
            }

我正在收到來自服務器的響應。 API POST 沒有問題。

您可以序列化您的響應並將其保存為文本文件。 它可以在任何時候或在您的應用程序中訪問。 這是一個簡單的演示,它只是從 Bing 的首頁收集了 html。 html 源現在已序列化並保存到示例應用程序可以在任何地方訪問的文件。

確保您的程序在部署時無需提升權限即可讀取和寫入文件(AppData 文件夾等)。 本地測試沒問題。 以下是您可以放入新控制台應用程序以進行測試的代碼。

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;



   namespace SerializeObject
{
    class Program
    {
        static void Main(string[] args)
        {
            //ensure you delete StorageFile.txt if you encounter errors or change structure of MyObject 
            GetInternetString("http://www.bing.com");
        }

        private static void GetInternetString(string url)
        {
            using (var wb = new WebClient())
            {
                var response = wb.DownloadString(url);
                Serialize(response);
            }
        }

        private static void Serialize(string webPageContent)
        {
            try
            {
                MyObject saveThis = new MyObject(webPageContent);
                IFormatter formatter = new BinaryFormatter();
                using (Stream stream = new FileStream("StorageFile.txt", FileMode.Append, FileAccess.Write))
                {
                    formatter.Serialize(stream, saveThis);
                    stream.Close();
                }

                Deserialize();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r" + ex.StackTrace);
            }
        }

        private static void Deserialize()
        {
            try
            {
                //change this to your specific use
                var lstSessionProjectFilesTrimsSaveThis = new List<MyObject>();
                using (var fileStream = new FileStream("StorageFile.txt", FileMode.Open))
                {
                    var bFormatter = new BinaryFormatter();
                    while (fileStream.Position != fileStream.Length)
                    {
                        //change this to your specific use
                        lstSessionProjectFilesTrimsSaveThis.Add((MyObject)bFormatter.Deserialize(fileStream));
                    }
                }

                foreach (var VARIABLE in lstSessionProjectFilesTrimsSaveThis)
                {
                    Console.WriteLine(VARIABLE.WebPageContent);
                }

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
        }



[Serializable] //mandatory to have this declaration
    public class MyObject
    {
        public MyObject(string webPageContent)
        {
            WebPageContent = webPageContent;
        }

        public string WebPageContent { get; set; }
    }
}
}

暫無
暫無

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

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