473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Download File From Content-Disposition

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().GetResponseS tream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();

// download with stream (not working)
string d = "https://www.login.com?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);
}

Aug 1 '06 #1
2 3233
check this out to see if related to your problem:

http://support.microsoft.com/kb/279667/en-us

-t

ro***********@gmail.com ha scritto:
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().GetResponseS tream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();

// download with stream (not working)
string d = "https://www.login.com?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);
}
Aug 1 '06 #2
no it doesn't ,
thanks any way

to**************@uniroma1.it wrote:
check this out to see if related to your problem:

http://support.microsoft.com/kb/279667/en-us

-t

ro***********@gmail.com ha scritto:
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().GetResponseS tream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();

// download with stream (not working)
string d = "https://www.login.com?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);
}
Aug 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BOHICA | last post by:
I would like on my page when users click one of my mp3 files for the dialog box to pop up so they can choose to save the file to disk instead of trying to open through IE, but the dang thing won't...
6
by: Topmind | last post by:
If the Windows Web browser (IE 5 or 6) opens an XLS file (spreadsheet), how it handles it depends on a file association setting within Windows. There is a setting called "confirm open after...
8
by: lawrence | last post by:
Under the domain publicpen.com I've several dozen sites in subdiretories, such as www.publicpen.com/honenbeger. I've no trouble with any of these sites. But under one, which I put in yesterday,...
6
by: Bryan Martin | last post by:
Ok im trying to figure out how internet explorers file download box always seem to at least come close to knowing the exact file size/time to download. From what I can tell its getting its not...
0
by: Sarah Akers | last post by:
GgF ----gL5cJ72EqiGIQ0SK65Rz Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <head> <style type=3D"text/css">.eyebrow { FONT-WEIGHT: bold; FONT-SIZE: 10px; TE=
0
by: Anne Snow | last post by:
7qs ----bSCMEc0pLIbJxmV9 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html><head><style type=3Dtext/css>.eyebrow { FONT-WEIGHT: bold; FONT-SIZE= : 10px;...
5
by: Tim_Mac | last post by:
hi, i read that by adding the following code to by aspx pages, it would not store temporary internet files: Response.Cache.SetCacheability(HttpCacheability.NoCache); it didn't actually work...
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
3
by: rafi | last post by:
Hi, I developed a website using php5 & mysql5. One of its features is the ability of users to upload files to the server, and allow other users to view these files. I allow uploading the...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.