0

To download file using web browser i have to use VPN or to be connected to specific WiFi network. So lets presume, that i am connected to that specific WiFI. So the procedure to download file using url is like this:

  1. I need to open specific website (lets call it www.abc.com/start) in browser (i dont need to enter any credentials just to load and nothing to do).
  2. Then i can open www.abc.com/prictureA and download it. If i dont open www.abc.com/start and go directly to www.abc.com/prictureA i get info - NOT AUTHENTICATED.

Once i opened in browser www.abc.com/start, i can open www.abc.com/prictureB www.abc.com/prictureC and so on unlimited times (maybe 1 or 2 hours?).

If i use this:

WebClient webClient = new WebClient();
webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36");
Uri remoteFileUrlStart = new Uri("www.abc.com/start");
webClient.DownloadFileAsync(remoteFileUrlStart, "test.jpg");

I get test.jpg saying not authenticated. How to open first www.abc.com/start programatically before downloading www.abc.com/start?

Aer
  • 49
  • 1
  • 12

2 Answers2

0

I made an example to use cookies:

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("www.abc.com/start");
wr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36";
HttpWebResponse authresponse = (HttpWebResponse)wr.GetResponse();
HttpWebRequest wr2 = (HttpWebRequest)WebRequest.Create("www.abc.com/prictureA");
foreach (Cookie c in authresponse.Cookies)
    wr2.CookieContainer.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
HttpWebResponse imageresponse = (HttpWebResponse)wr2.GetResponse();
using (Stream str = imageresponse.GetResponseStream())
{
    byte[] buffer = new byte[str.Length];
    str.Read(buffer, 0, buffer.Length);
    //save image
    File.WriteAllBytes("prictureA.jpg", buffer);
}
thehennyy
  • 4,020
  • 1
  • 22
  • 31
bdn02
  • 1,500
  • 9
  • 15
  • I always thought the webclient could handle cookies but it seems it can not, so i deleted my answer. When using webrequests you dont have to copy the cookies manually, just use the same cookiecontainer for all requests. – thehennyy Jan 12 '16 at 11:02
  • Look this: http://stackoverflow.com/questions/1777221/using-cookiecontainer-with-webclient-class – bdn02 Jan 12 '16 at 11:06
  • yes, i found a similar approach a moment ago. personally i always use webrequests for more flexibility. – thehennyy Jan 12 '16 at 11:08
0

I used suggestion in a comment to use cookies. My solution was to use WebClient with cookies created by HttpWebRequest. WebClient does not have native cookies support so i used class WebClientEx (from Using CookieContainer with WebClient class)

public class WebClientEx : WebClient
{
    public WebClientEx(CookieContainer container)
    {
        this.container = container;
    }

    public CookieContainer CookieContainer
    {
        get { return container; }
        set { container = value; }
    }

    private CookieContainer container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest r = base.GetWebRequest(address);
        var request = r as HttpWebRequest;
        if (request != null)
        {
            request.CookieContainer = container;
        }
        return r;
    }

    protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
    {
        WebResponse response = base.GetWebResponse(request, result);
        ReadCookies(response);
        return response;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        ReadCookies(response);
        return response;
    }

    private void ReadCookies(WebResponse r)
    {
        var response = r as HttpWebResponse;
        if (response != null)
        {
            CookieCollection cookies = response.Cookies;
            container.Add(cookies);
        }
    }
}
Community
  • 1
  • 1
Aer
  • 49
  • 1
  • 12