Windows Develop Bookmark and Share   
 index > Windows Forms General > copying a webpage toa local file using c#
 

copying a webpage toa local file using c#

Hi all,
I am developing a windows form where a .csv file needs to be accessed from the net and dump its contents in database.
Since the site does not have downloading option ,i am only left with an option to copy the contents of the .csv file when it is displayed as webpage.
For this i am using the following code,


HttpWebRequest objWebReq =(HttpWebRequest)WebRequest.Create("http://www.yoursite.com");
HttpWebResponse objWebResp = (HttpWebResponse)objWebReq.GetResponse();
StreamReader objStream = new StreamReader(objWebResp.GetResponseStream(), System.Text.Encoding.ASCII);
//read the data
string sResp = objStream.ReadToEnd();

if (sResp.Length > 0)

{
FileStream fsout = new
FileStream("myfile.txt", FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fsout);
sw.Write(sResp);
}

it works fine ,but the problem is it does not copy the whole webpage.

i tried using stringbuilder to copy the stream but no use.


please anyone help me with this
shaviraghu  Thursday, March 05, 2009 1:28 PM

Are you closing your StreamWriter? I don't see sw.Close(). This is necessary to flush the buffer to disk.

Minimally, the objWebResp should be closed as well. The objWebResp is rather important because by default your application can only open two simultaneous connections to another machine (configuration/system.net/connectionManagement in the app.config file). I'd recommend the "using" keyword to ensure proper cleanup even if an exception occurs.

using(HttpWebResponseobjWebResp=(HttpWebResponse)objWebReq.GetResponse())
{
//Codehere.
}



  • Marked As Answer byshaviraghu Friday, March 06, 2009 5:35 AM
  •  
BinaryCoder  Thursday, March 05, 2009 11:43 PM

Are you closing your StreamWriter? I don't see sw.Close(). This is necessary to flush the buffer to disk.

Minimally, the objWebResp should be closed as well. The objWebResp is rather important because by default your application can only open two simultaneous connections to another machine (configuration/system.net/connectionManagement in the app.config file). I'd recommend the "using" keyword to ensure proper cleanup even if an exception occurs.

using(HttpWebResponseobjWebResp=(HttpWebResponse)objWebReq.GetResponse())
{
//Codehere.
}



  • Marked As Answer byshaviraghu Friday, March 06, 2009 5:35 AM
  •  
BinaryCoder  Thursday, March 05, 2009 11:43 PM
hi binary coder,
thank u for ur help.The problem was with closing streamwriter.i closed sw and it is workin fine.
thank u once again.

raghu
shaviraghu  Friday, March 06, 2009 5:35 AM

You can use google to search for other answers

Custom Search

More Threads

• Programatically reading lines from multi-line textbox
• PHP Editor
• Changing Form size at run-time
• Drag drop datagrid rows in different forms
• How to read from dll.config when hosting UserControls in IE
• Treeview node cropping
• reportviewer - jump to a specific line of the Table
• Problem with C++ Forms in .net 2003
• Creating and using user.config
• Invoke and MDI Child Forms