Hi to everyone !!Specially to Mahesh :)
I want to login to a url and download pdf files
i used WebRequest.Credetials method but still unable to get the destination url
?????
Any help will be really appreciated
Loading
Hi to everyone !!Specially to Mahesh :)
I want to login to a url and download pdf files
i used WebRequest.Credetials method but still unable to get the destination url
?????
Any help will be really appreciated
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
rahul bhosalePosted Jan 23, 2009, 10:03 AM
hi,
this may help u....
public
bool DownloadFile(string sourcePath, string destPath){
// Function will return the number of bytes processed // to the caller. Initialize to 0 here. int bytesProcessed = 0; // Assign values to these objects here so that they can // be referenced in the finally block Stream remoteStream = null; Stream localStream = null; WebResponse response = null; // Use a try/catch/finally block as both the WebRequest and Stream // classes throw exceptions upon error try{
// Create a request for the specified remote file name WebRequest request = WebRequest.Create(FilePath); if (request != null){
// Send the request to the server and retrieve the // WebResponse objectresponse = request.GetResponse();
if (response != null){
// Once the WebResponse object has been retrieved, // get the stream object associated with the response's dataremoteStream = response.GetResponseStream();
// Create the local filelocalStream =
File.Create(Application.StartupPath + @"\" + destPath); // Allocate a 1k buffer byte[] buffer = new byte[1024]; int bytesRead;// Simple do/while loop to read from stream until
// no bytes are returned do{
//prgrssBarUpdatedVersionDownlaod.Value += 10; // Read data (up to 1k) from the streambytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local filelocalStream.Write(buffer, 0, bytesRead);
// Increment total bytes processedbytesProcessed += bytesRead;
}
while (bytesRead > 0);}
}
return true;}
catch (WebException ){
MessageBox.Show("Error"); return false;}
catch (Exception Ex){
MessageBox.Show(Ex.Message,
); return false;}
finally{
// Close the response and streams objects here // to make sure they're closed even if an exception // is thrown at some point if (response != null) response.Close(); if (remoteStream != null) remoteStream.Close(); if (localStream != null) localStream.Close();}