473,397 Members | 1,972 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,397 software developers and data experts.

webclient.downloadfile

mk
Hi,
I am trying to download a file from a remote webserver. I create the
following:

string username="remoteusername";
string password="remotepassword";
string URI="http://remote.target.server/getdocument.asp?ID=sometest.pdf";

NetworkCredential nc = new NetworkCredential(username, password);
WebRequest req = WebRequest.Create(URI);
req.Credentials = nc;
WebResponse resp = req.GetResponse();
using(FileStream fsb = new FileStream(filename, FileMode.CreateNew))
{
BinaryWriter bw = new BinaryWriter(fsb);
Byte[] read = new Byte[1024];
int bytes = resp.GetResponseStream().Read(read, 0, 1024);
...
// Carry on and stream the bytes to a local file


If I set the URI to a file in a directory on my localhost webserver (and
change the username and password) this works a treat. When I try to download
the remote file, I get 403 - Access forbidden. Unfortunately if a just enter
the URI into IE, I can view the file on screen. So I know I have permission,
I know the file exists, and I know the code works coz it works for the local
file.
Can anyone tell me where I'm going wrong?

Thanks,

Martin
Nov 16 '05 #1
1 4687
I'd recommend 2 things:

1) Read the whole message returned by the webserver, i.e. catch the
WebException that is thrown during the call to GetResponse, and use
the Response property of the WebException to get the whole answer.
The web server may actually be kind enough to provide information.
Here's a quickly hacked example:

try
{
WebResponse resp = req.GetResponse();
}
catch (WebException e)
{
Stream respStream = e.Response.GetResponseStream();
byte[] buffer = new byte[8192];
int amt = respStream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.ASCII.GetString(buffer, 0, amt));
return;
}

2) Use the information obtained from 1). It might be as simple as

req.CookieContainer = new CookieContainer();

Happy hunting,
Ras

"mk" <mk@discussions.microsoft.com> wrote in message news:<C6**********************************@microso ft.com>...
Hi,
I am trying to download a file from a remote webserver. I create the
following:

string username="remoteusername";
string password="remotepassword";
string URI="http://remote.target.server/getdocument.asp?ID=sometest.pdf";

NetworkCredential nc = new NetworkCredential(username, password);
WebRequest req = WebRequest.Create(URI);
req.Credentials = nc;
WebResponse resp = req.GetResponse();
using(FileStream fsb = new FileStream(filename, FileMode.CreateNew))
{
BinaryWriter bw = new BinaryWriter(fsb);
Byte[] read = new Byte[1024];
int bytes = resp.GetResponseStream().Read(read, 0, 1024);
...
// Carry on and stream the bytes to a local file


If I set the URI to a file in a directory on my localhost webserver (and
change the username and password) this works a treat. When I try to download
the remote file, I get 403 - Access forbidden. Unfortunately if a just enter
the URI into IE, I can view the file on screen. So I know I have permission,
I know the file exists, and I know the code works coz it works for the local
file.
Can anyone tell me where I'm going wrong?

Thanks,

Martin

Nov 16 '05 #2

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

Similar topics

1
by: James Write | last post by:
Hi, I am using the webclient.downloadfile() method to download a file from a remote web server to the local harddrive. public CopyFile(string sSrcPath, string sDestPath) { WebClient oClient...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
1
by: Cheri Elkin | last post by:
I recieve the following error when attempting to download a file from our web server to be placed on our hard drive. Any clues as to why I would get this error? By the way, the directory already...
8
by: DEWright_CA | last post by:
Why does WebClient.DownloadFile loose my completed path? Ok, I have a function in my app then when my button is clicked it checks to see if the files come from a local drive or a http address....
1
by: peter.rietmann | last post by:
I have been suddenly having a performance problem downloading images from one server (productive) to another (productive) using the following code .. WebClient webClient = new WebClient(); try...
2
by: Lila Godel | last post by:
I am having a problem with the download of web pages via the WebClient.DownloadFile function in the specialized VB.Net 2003 I.E. plug-in I am designing to speed up the work on my latest project. ...
1
by: David Satz | last post by:
Hello--I just upgraded to Visual Studio .NET 2005 and suddenly, all my .NET 1.1 applications that accessed Web sites have broken. For example, this code: WebClient wc = new WebClient();...
1
by: vin.dor | last post by:
Dear All, I am using WebClient in my Visual Studio .Net 2003 project to download an image from the Internet. The following is my function: C# Code: public static bool downloadFile(string...
4
by: gavkel | last post by:
Can anyone get this to work? All Im getting is the login html being downloaded. Would appreciate any help - my heads hurts Thanks Gav
8
by: =?Utf-8?B?UnVpIE9saXZlaXJh?= | last post by:
WebClient.DownloadFile I am using the WebClient.DownloadFile function to download a file from server to a local client. When I execute the below code, file is created in server and not in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.