List Info

Thread: C-Sharp (C#) Group: Download File From Content-Disposition




C-Sharp (C#) Group: Download File From Content-Disposition
user name
2006-08-01 13:15:11
Hi,
I have a program that connects to a site With WebRequest and
WebResponse .
The response of this site is a file (csv file).
The problem is that the file do not comes as a stream , hi
is a part of
the header
(Content-Disposition:attachment; filename=changehistory.csv)
i tried to download this file as a stream , but it do not
work .
i triesd with WebClient , but i coudn't attache a
cookieContainer to
him . (Connectiong to LogIn)
I did a search in the web an again i found nothing .

Please Help me , Rony

My Code is :
 #region SaveStreamToFile
   /// <summary>
   /// Saves the stream on the HD to the path provided.
   /// </summary>
   /// <param name="filePath">Where to
save the file. (includes the
file name)</param>
   /// <param name="stream">The stream
contains the data to be
saved.</param>
   /// <returns>True - Saved successfully. Otherwise
False.</returns>
   public  void SaveStreamToFile(string filePath, Stream
stream)
   {
       // If not all data was provided return false;
       //if (string.IsNullOrEmpty(filePath) || stream ==
null)
       //    return false;
      // System.IO.Stream gzStream = stream; //= new
System.IO.Stream()//(stream,
System.IO.Compression.CompressionMode.Decompress);
       FileStream fs = null;
       try
       {
           byte[] inBuffer = new byte[10000];
           int bytesRead = 0;
           fs = File.Open(filePath, FileMode.Create,
FileAccess.Write,
FileShare.Read);
           while ((bytesRead = stream.Read(inBuffer, 0,
inBuffer.Length)) > 0)
           {
               fs.Write(inBuffer, 0, bytesRead);
           }
           fs.Flush();
          // return true;
       }
       catch (Exception ex)
       {
           throw ex;
         //  ExceptionPolicy.HandleException(ex,
"Global Policy");
           //return false;
       }
       finally
       {
           fs.Close();
       }
   }
   #endregion

   protected void Go1()
   {
       string get = "http://www
.login.com?Usr=rony&Psw=vava";
       CookieContainer cookies = new CookieContainer();
       HttpWebRequest webRequest = WebRequest.Create(get) as
HttpWebRequest;
       webRequest.ContentType =
"application/x-www-form-urlencoded";
       webRequest.CookieContainer = cookies;
       WebResponse myResponse = webRequest.GetResponse();
       Stream ReceiveStream =
myResponse.GetResponseStream();
       Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");
       StreamReader readStream = new
StreamReader(ReceiveStream,
encode);

       string str = readStream.ReadToEnd();
       int pos = str.LastIndexOf("href=");
       int p = str.IndexOf("target", pos);

       string s = str.Substring(pos + 6, p - pos - 4);
       int v = s.IndexOf('"');
       string sv = s.Substring(0, v);
       sv = sv.Replace("&amp;",
"&");

       readStream.Close();
       // now we can send out cookie along with a request
for the
protected page
       webRequest = WebRequest.Create(sv) as HttpWebRequest;
       webRequest.CookieContainer = cookies;
       StreamReader responseReader = new
StreamReader(webRequest.GetResponse().GetResponseStream());
       string responseData = responseReader.ReadToEnd();
       responseReader.Close();

       // download with stream (not working)
       string d = "https://www.login.c
om?download=32";
       HttpWebRequest webRequest1 = WebRequest.Create(d) as
HttpWebRequest;
       webRequest1.ContentType =
"application/x-www-form-urlencoded";
       webRequest1.CookieContainer = cookies;
       WebResponse myResponse1 = webRequest1.GetResponse();
       Stream ReceiveStream1 =
myResponse1.GetResponseStream();
       string aa =
myResponse1.Headers["Content-Disposition"];
       SaveStreamToFile("C:\file.csv",
ReceiveStream1);
   }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )