0

I used following POST request in my C# WPF project: https://stackoverflow.com/a/8091963/17283265 . So i'm getting user login Bearer id and i want to save it (I use localstorage to save it like redux in reactjs). 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.

suakinos
  • 13
  • 6

1 Answers1

0

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. The html source is now serialized and saved to file that can be accessed anywhere by the sample app.

Ensure your program can read and write to the file without elevated permissions when deploying (AppData folder etc). 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; }
    }
}
}
DaveCS
  • 950
  • 1
  • 10
  • 16
  • Both `WebClient` and `BinaryFormatter` should not be used, especially the later since it poses a security risk. – Kostas K. Dec 13 '21 at 12:02