title: How to save and reuse Cookies in C#?
body: <p>i`m trying to use cookies in my c# application which is consuming a web service. Login method is working but when i try to use change password method it gives 401 status code.</p>
  
  <p><strong>HTTP.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Net;
  using System.Text;
  using System.Threading.Tasks;
  using System.Web;
  using System.Web.Security;
  
  namespace RemoteSynchronizer
  {
      class HTTP
      {
          HttpWebRequest request = null;
          public static CookieContainer contain;
  
  
          public HTTP()
          {
              contain = new CookieContainer();
          }
  
          public int postData(string url, StringBuilder data)
          {
  
              int status;
              Uri address = new Uri(url);
  
              request = WebRequest.Create(address) as HttpWebRequest;
              request.CookieContainer = contain;
  
              request.Method = "POST";
              request.ContentType = "application/x-www-form-urlencoded";
              request.KeepAlive = true;
  
              byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
  
              request.ContentLength = byteData.Length;
  
              using (Stream postStream = request.GetRequestStream())
              {
                  postStream.Write(byteData, 0, byteData.Length);
              }
  
              using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
              {
                  status = (int)response.StatusCode;
              }
  
  
              return status;
          }
  
  
  /// &lt;summary&gt;
  /// /////////////////////////
  /// &lt;/summary&gt;
  /// &lt;param name="url"&gt;&lt;/param&gt;
  /// &lt;param name="data"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  /// 
  
          public int change(string url, StringBuilder data)
          {
              int status;
              Uri address = new Uri(url);
  
              request = WebRequest.Create(address) as HttpWebRequest;
  
              request.Method = "POST";
              request.ContentType = "application/x-www-form-urlencoded";
              request.Credentials = CredentialCache.DefaultCredentials;
              request.CookieContainer = contain;
  
              byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
  
              request.ContentLength = byteData.Length;
  
              using (Stream postStream = request.GetRequestStream())
              {
                  postStream.Write(byteData, 0, byteData.Length);
              }
  
              using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
              {
                  status = (int)response.StatusCode;
              }
  
              return status;
          }
      }
  }
  </code></pre>
  
  <p>How can i save cookie to reuse in other class?</p>