title: How to Download the File using HttpWebRequest and HttpWebResponse class(Cookies,Credentials,etc.)
body: <p>Thanks icktoofay, 
  I tried using <code>HttpWebRequest</code> and <code>HttpWebResponse</code>. </p>
  
  <p>When I request for URL by passing the Credentials like UserName And Password. </p>
  
  <p>I will get the Session Id Back in the Response. </p>
  
  <p>After getting that Session Id, How to Move Further. </p>
  
  <p>The authenticated user are tracked using credentials/cookies. 
  I'm Having the Exact Url of the File to be downloaded and credentials. 
  If you want to use Cookies I will. I need to read the File Data and write/save it in a Specified Location. </p>
  
  <p>The code I'm using is;</p>
  
  <pre><code>string username = "";
  string password = "";
  string reqString = "https://xxxx.com?FileNAme=asfhasf.mro" + "?" + 
               "username=" + username + &amp;password=" + password;
  byte[] requestData = Encoding.UTF8.GetBytes(reqString);
  string s1;
  CookieContainer cc = new CookieContainer();
  
  var request = (HttpWebRequest)WebRequest.Create(loginUri);
  request.Proxy = null;
  request.CookieContainer = cc;
  request.Method = "POST";
  HttpWebResponse ws = (HttpWebResponse)request.GetResponse();
  Stream str = ws.GetResponseStream();
  //ws.Cookies
  //var request1 = (HttpWebRequest)WebRequest.Create(loginUri);
   byte[] inBuf = new byte[100000];
  int bytesToRead = (int) inBuf.Length;
  int bytesRead = 0;
  while (bytesToRead &gt; 0) 
  {
      int n = str.Read(inBuf, bytesRead,bytesToRead);
      if (n==0)
      break;
      bytesRead += n;
      bytesToRead -= n;
  }
  FileStream fstr = new FileStream("weather.jpg", FileMode.OpenOrCreate, 
                                       FileAccess.Write);
  fstr.Write(inBuf, 0, bytesRead);
  str.Close();
  fstr.Close();
  </code></pre>