HttpWebRequest

  • Thread starter Thread starter Steve B.
  • Start date Start date
Ok, I can post the code, but I don't think it will be usefull.
I paste below both client and server side. I don't think the server side is
wrong (an ashx handler that write raw file to the output) since I can
donwload the file correctly using IE.

An other precision : this code worked well when it was build with .NET 1.1
(now we are using .NET 2.0)

The client side, a download method :

public void DownloadFile(
string url,
string localPath,
bool createFolder
)
{
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(
url
);
req.Credentials = new NetworkCredential(
txtUserName.Text,
txtPassword.Text
); // Require since it use basic or NTLM

HttpWebResponse response = (HttpWebResponse) req.GetResponse();
BinaryReader br = new BinaryReader(response.GetResponseStream());

if(createFolder)
Directory.CreateDirectory(localPath);
string fileName = Path.GetFileName(url);

FileStream fs = new FileStream(
Path.Combine(localPath, fileName),
FileMode.Create,
FileAccess.Write
);
byte[] buff = new byte[256];
int len = 0;
do
{
len = br.Read(buff,0,256);
fs.Write(buff,0,len);
}
while (len >0);
fs.Close();
response.Close();
}
catch(Exception exc)
{
throw exc;
}
}

And the server side, an ashx handler :

public void ProcessRequest(HttpContext context)
{
// Put user code to initialize the page here
string paramFileName = context.Request.Params["FileName"];
if (paramFileName != null && paramFileName.Length > 0)
{
WebService.VersionsService vs = new WebService.VersionsService(); // A Web
service to the file repository
vs.Credentials = System.Net.CredentialCache.DefaultCredentials;
string FileName = HttpUtility.UrlDecode(paramFileName);
context.Response.AddHeader(
"content-disposition",
"attachment; filename=" + FileName
);
context.Response.ContentType = "application/octet-stream";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] raw = vs.GetFileFromRepository(FileName); // Get raw file
ms.Write(raw, 0, raw.Length);
ms.Seek(0, System.IO.SeekOrigin.Begin);
byte[] buff = new byte[256];
int len = 0;
do
{
len = ms.Read(buff,0,256);
context.Response.OutputStream.Write(
buff,
0,
len
);
context.Response.Flush();
}
while (len >0);
//context.Response.End();
}
}
 
Thus wrote Steve B.,
Ok, I can post the code, but I don't think it will be usefull.

Agreed, though the ASHX code looks awkward. Why don't you write the "raw"
array directly to the reponse's output stream? Also, on the client side,
forget about BinaryReader, you can copy the reponse directly from the response
stream to the file stream.
I paste below both client and server side. I don't think the server
side is
wrong (an ashx handler that write raw file to the output) since I can
donwload the file correctly using IE.
An other precision : this code worked well when it was build with .NET
1.1 (now we are using .NET 2.0)

Can you use Fiddler or any other HTTP proxy to capture the network traffic?

Cheers,
 
Back
Top