Reading a file from external url – ASP .NET
Often times you might need to read a file from another website server. For example reading a inventory data file from a vendor website. Here is a sample code to achieve that.
WebResponse result = null;
string output = “”;try
{
WebRequest req = WebRequest.Create(“http://www.website.com/filename.txt”);
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding(“utf-8″);
StreamReader sr = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = sr.Read(read, 0, read.Length);
while (count > 0)
{
String str = new String(read, 0, count);
output += str;
count = sr.Read(read, 0, read.Length);
}
}
catch (Exception)
{
Response.Write(“get failed”);
}
if (result != null)
{
result.Close();
}Response.Write(output);










Leave your response!