Groups | Blog | Home
all groups > dotnet framework > february 2006 >

dotnet framework : HttpWebRequest


Steve B.
2/14/2006 8:43:50 PM
Hi,

I use a HttpWebRequest object to download a file.

It works well if I use http://myserver/file.ext

But if I use http://192.168.x.y/file.exe it throw an exception "Error:
protocol violation".

I use VS 2005 and .Net 2.0.

What is wrong ?

Thanks in advance.
Steve

Joerg Jooss
2/14/2006 8:51:28 PM
Thus wrote Steve B.,

[quoted text, click to view]

No code... no network trace... hard to tell ;-)

Cheers,
--
Joerg Jooss
news-reply@joergjooss.de

Steve B.
2/15/2006 12:00:00 AM
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();
}
}

"Joerg Jooss" <news-reply@joergjooss.de> a écrit dans le message de news:
94fc50711443d8c7ffc7f1bf8ca2@msnews.microsoft.com...
[quoted text, click to view]

Joerg Jooss
2/15/2006 7:39:06 PM
Thus wrote Steve B.,

[quoted text, click to view]

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.

[quoted text, click to view]

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

Cheers,
--
Joerg Jooss
news-reply@joergjooss.de

AddThis Social Bookmark Button