简体   繁体   English

C#.Net Framework WPF:如何进行 POST 调用并保存登录 Baerer 密钥?

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

I used following POST request in my C# WPF project: https://stackoverflow.com/a/8091963/17283265 .我在我的 C# WPF 项目中使用了以下 POST 请求: https://stackoverflow.com/a/8091963/17282.3 So i'm getting user login Bearer id and i want to save it (I use localstorage to save it like redux in reactjs).所以我正在获取用户登录 Bearer id,我想保存它(我使用 localstorage 来保存它,就像 reactjs 中的 redux 一样)。 Same pages requre to authenticate.相同的页面需要进行身份验证。 I didn't find a good solution for this.我没有找到一个好的解决方案。 Here is my code.这是我的代码。

            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;
            }

I'm getting response from server.我正在收到来自服务器的响应。 No problem with API POST. API POST 没有问题。

You can serialize your response and save it as a text file.您可以序列化您的响应并将其保存为文本文件。 It can be accessed at anytime or place in your application.它可以在任何时候或在您的应用程序中访问。 Here is a simple demo that just collects the html from the front page of Bing.这是一个简单的演示,它只是从 Bing 的首页收集了 html。 The html source is now serialized and saved to file that can be accessed anywhere by the sample app. html 源现在已序列化并保存到示例应用程序可以在任何地方访问的文件。

Ensure your program can read and write to the file without elevated permissions when deploying (AppData folder etc).确保您的程序在部署时无需提升权限即可读取和写入文件(AppData 文件夹等)。 Local testing is fine.本地测试没问题。 Here is code you can drop into a new Console app for testing purposes.以下是您可以放入新控制台应用程序以进行测试的代码。

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