|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
HttpWebRequestHi,
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 Thus wrote Steve B.,
> Hi, No code... no network trace... hard to tell ;-)> > 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 ? Cheers, -- Joerg Jooss news-re***@joergjooss.de 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-re***@joergjooss.de> a écrit dans le message de news: 94fc50711443d8c7ffc7f1bf8***@msnews.microsoft.com...Show quote > Thus wrote Steve B., > >> 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 ? > > No code... no network trace... hard to tell ;-) > > Cheers, > -- > Joerg Jooss > news-re***@joergjooss.de > > 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 Can you use Fiddler or any other HTTP proxy to capture the network traffic?> 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) Cheers, -- Joerg Jooss news-re***@joergjooss.de |
|||||||||||||||||||||||