简体   繁体   English

使用浏览器下载文件之前,需要先打开特定的网站。 如何以编程方式使用C#实现此目的?

[英]Using browser need to open specific website first before downloading file. How to programatically using c# implement this?

To download file using web browser i have to use VPN or to be connected to specific WiFi network. 要使用网络浏览器下载文件,我必须使用VPN或连接到特定的WiFi网络。 So lets presume, that i am connected to that specific WiFI. 因此,假设我已连接到该特定的WiFI。 So the procedure to download file using url is like this: 因此,使用url下载文件的过程如下:

  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). 我需要在浏览器中打开特定的网站(我叫它www.abc.com/start)(我不需要输入任何凭据即可加载,而无需执行任何操作)。
  2. Then i can open www.abc.com/prictureA and download it. 然后,我可以打开www.abc.com/prictureA并下载它。 If i dont open www.abc.com/start and go directly to www.abc.com/prictureA i get info - NOT AUTHENTICATED. 如果我不打开www.abc.com/start并直接转到www.abc.com/prictureA,我会得到信息-未授权。

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?). 在浏览器www.abc.com/start中打开后,就可以无限期地打开www.abc.com/prictureB www.abc.com/prictureC,等等(也许1或2个小时?)。

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. 我得到test.jpg说未经验证。 How to open first www.abc.com/start programatically before downloading www.abc.com/start? 在下载www.abc.com/start之前,如何以编程方式首先打开www.abc.com/start?

I made an example to use cookies: 我举了一个使用cookie的例子:

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);
}

I used suggestion in a comment to use cookies. 我在评论中使用了建议来使用Cookie。 My solution was to use WebClient with cookies created by HttpWebRequest. 我的解决方案是将WebClient与HttpWebRequest创建的cookie一起使用。 WebClient does not have native cookies support so i used class WebClientEx (from Using CookieContainer with WebClient class ) WebClient不支持本机cookie,因此我使用了WebClientEx类 (来自对WebClient类使用CookieContainer

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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM