Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. HttpWebRequest and HttpWebResponse with .aspx files

HttpWebRequest and HttpWebResponse with .aspx files

Scheduled Pinned Locked Moved ASP.NET
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kanjinghat
    wrote on last edited by
    #1

    Dear All, I am in a need to read .aspx and .config files from a remote system. For that i have used the following method. public string DownloadFile(string downloadUrl) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl); req.Method = "GET"; HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); Stream respStream = resp.GetResponseStream(); StreamReader rdr = new StreamReader(respStream); string inLine = rdr.ReadLine(); string strResult=string.Empty; while (inLine != null) { inLine = rdr.ReadLine(); strResult +=inLine; } rdr.Close(); return strResult; } In the above code if i provide URI of a text fiel in a remot system the method reads the file and returns me the content. But the same is not working if i provide URI of a .aspx file. Can any body help me ThanS in advance

    Ramesh.Kanjinghat

    M 1 Reply Last reply
    0
    • K Kanjinghat

      Dear All, I am in a need to read .aspx and .config files from a remote system. For that i have used the following method. public string DownloadFile(string downloadUrl) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl); req.Method = "GET"; HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); Stream respStream = resp.GetResponseStream(); StreamReader rdr = new StreamReader(respStream); string inLine = rdr.ReadLine(); string strResult=string.Empty; while (inLine != null) { inLine = rdr.ReadLine(); strResult +=inLine; } rdr.Close(); return strResult; } In the above code if i provide URI of a text fiel in a remot system the method reads the file and returns me the content. But the same is not working if i provide URI of a .aspx file. Can any body help me ThanS in advance

      Ramesh.Kanjinghat

      M Offline
      M Offline
      Mark J Miller
      wrote on last edited by
      #2

      I ran your code from a console application and it seems to work fine for aspx files. I've pasted below my test app based on your code. I requested several different aspx files on my localhost and one from a public url and all were succussful. Can you reach the pages using your web browser? Also, if you are trying to read .config files you will run into a problem there as ASP.NET will deny all requests for config files. There is a possible way around it, but it would be a big mistake as it would open up a huge security hole giving the public access to your connection strings and other sensitive config info. using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string input = string.Empty; while (0 != string.Compare(input, "exit", true)) { if (!string.IsNullOrEmpty(input)) { Console.WriteLine(DownloadFile(input)); } Console.Write("\r\nUrl: "); input = Console.ReadLine(); } } public static string DownloadFile(string downloadUrl) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl); req.Method = "GET"; HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream respStream = resp.GetResponseStream(); StreamReader rdr = new StreamReader(respStream); string inLine = rdr.ReadLine(); string strResult = string.Empty; while (inLine != null) { inLine = rdr.ReadLine(); strResult += inLine; } rdr.Close(); return strResult; } } }

      K 1 Reply Last reply
      0
      • M Mark J Miller

        I ran your code from a console application and it seems to work fine for aspx files. I've pasted below my test app based on your code. I requested several different aspx files on my localhost and one from a public url and all were succussful. Can you reach the pages using your web browser? Also, if you are trying to read .config files you will run into a problem there as ASP.NET will deny all requests for config files. There is a possible way around it, but it would be a big mistake as it would open up a huge security hole giving the public access to your connection strings and other sensitive config info. using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string input = string.Empty; while (0 != string.Compare(input, "exit", true)) { if (!string.IsNullOrEmpty(input)) { Console.WriteLine(DownloadFile(input)); } Console.Write("\r\nUrl: "); input = Console.ReadLine(); } } public static string DownloadFile(string downloadUrl) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl); req.Method = "GET"; HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream respStream = resp.GetResponseStream(); StreamReader rdr = new StreamReader(respStream); string inLine = rdr.ReadLine(); string strResult = string.Empty; while (inLine != null) { inLine = rdr.ReadLine(); strResult += inLine; } rdr.Close(); return strResult; } } }

        K Offline
        K Offline
        Kanjinghat
        wrote on last edited by
        #3

        Thanks Mark , Well it is working fine for me too. But the exception is that it only reads .aspx files of an application if i copy an .aspx file from a web application and store it in a folder, which is set with web sharing for every one, then it fails to read the content. It will be very help ful for me if you let me the work around to be followed to read .config file. ThanS in advance.;)

        Ramesh.Kanjinghat

        M 1 Reply Last reply
        0
        • K Kanjinghat

          Thanks Mark , Well it is working fine for me too. But the exception is that it only reads .aspx files of an application if i copy an .aspx file from a web application and store it in a folder, which is set with web sharing for every one, then it fails to read the content. It will be very help ful for me if you let me the work around to be followed to read .config file. ThanS in advance.;)

          Ramesh.Kanjinghat

          M Offline
          M Offline
          Mark J Miller
          wrote on last edited by
          #4

          Kanjinghat wrote: it only reads .aspx files of an application if i copy an .aspx file from a web application and store it in a folder, which is set with web sharing for every one Not sure what you mean here. Are you trying to read the source of the aspx files? Or just the output? And are you requesting the files from an HTTP server (IIS) or trying to use file sharing? If you're trying to use HttpWebRequest to get the source of the files from a windows share you're using the wrong methods. If this is the case then you need to be using FileStream objects, not request or response objects. When you request files from a share over a network you don't use a web browser, you use windows explorer. Using the WebRequest objects is the equivelent of using your web browser. If you want the equivelent of windows explorer you use the FileStream objects. Also, this means you won't need to open up a security hole in your web application by exposing your config files publicly. Kanjinghat wrote: It will be very help ful for me if you let me the work around to be followed to read .config file. the default setting for .config files is in the httpHanlders section. It's defined as follows: In order to provide public access you'll need to change the handler for config files. I've never tried it, so I'm not positive which one. But my first guess would be System.Web.StaticFileHandler. If not you may have to roll your own handler which opens the files and spits out the contents to the response steam. But as a disclaimer, I want to say exposing your config files like this is a really bad idea. If you need programmatic access to the content of remote config files I would suggest using FTP instead. .Net doesn't have any built-in FTP libraries, but I've seen some free ones out on the web.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups