2

Is there a way to make the following function work via proxy?

public T[] ReadStream(System.IO.TextReader reader);

I want to be able to proxify the reader instance, so that it could download a file from the web on reading attempts and cache it somewhere.

Or maybe there is something default for this purpose?

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148

3 Answers3

8

Use WebClient.DownloadFile. If you need a proxy, you can set the Proxy property of your WebClient object.

Here's an example:

using (var client = new WebClient())
{
    client.Proxy = new WebProxy("some.proxy.com", 8000);
    client.DownloadFile("example.com/file.jpg", "file.jpg");
}

You can also download the file by parts with a BinaryReader:

using (var client = new WebClient())
{
    client.Proxy = new WebProxy("some.proxy.com", 8000);

    using (var reader = new BinaryReader(client.OpenRead("example.com/file.jpg")))
    {
        reader.ReadByte();
        reader.ReadInt32();
        reader.ReadBoolean();

        // etc.
    }
}
  • What you suggest is the obvious way. When I was talking about `proxy`, I meant some object that would sequentially cache the file from the web and reroute all `read`-like calls from the appropriate interface to that cached piece of file. So that I could say `TextReader t = new MyCustomWebReader("http://www.site.com/file.zip");` and it would read it (with some pauses, obviously). – Yippie-Ki-Yay Apr 08 '11 at 21:52
  • I'm not sure to understand what you ask for. –  Apr 08 '11 at 21:56
  • Similiar to this, where `StreamReader` is being constructed on top of web query: http://stackoverflow.com/questions/4842038/streamreader-and-reading-an-xml-file – Yippie-Ki-Yay Apr 08 '11 at 21:58
  • I want the same for arbitrary remote files. – Yippie-Ki-Yay Apr 08 '11 at 21:58
  • Not necessary proxified, just simply wrap a query in `StreamReader` so that it downloads and reads the file sequentially. – Yippie-Ki-Yay Apr 08 '11 at 22:05
1

Perhaps this is what you want? I am also slightly confused by the wording of the question, given your comments to the previous answer.

public StreamReader GetWebReader(string uri)
{
    var webRequest = WebRequest.Create(uri);
    var webResponse = webRequest.GetResponse();
    var responseStream = webResponse.GetResponseStream();
    return new StreamReader(responseStream);
}
0
var webRequestObject = (HttpWebRequest) WebRequest.Create("http://whatever");
var response = webRequestObject.GetResponse();
var webStream = response.GetResponseStream();
// Ta-da.
var reader = new StreamReader(webStream);
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148