下载登录墙C#后面的文件

时间:2016-09-08 12:34:49

标签: c# login downloading system.net

现在已经被困在这里一段时间了。 在SO上找到了很少的链接,但对我来说不起作用...... 在答案中,代码是在没有评论的情况下给出的,因为我这是第一次做这个我没有得到它... 并且无法让它工作,给我403 forbiden错误。

C# download file from the web with login

C# https login and download file

http://codesimplified.blogspot.hr/2013/11/asynchronous-file-download-from-web.html

这是我的代码(部分是复制粘贴,做了一些研究,所以我把评论作为我的思维方式 - >不确定它们是否正确)

        private void button_Click(object sender, RoutedEventArgs e)
    {
        logtsk.Start(); // logtsk = new Task(Login) first time using async methods too (did research, probs there's a better way)
    }

    private async void Login()
    {
        using (var handler = new HttpClientHandler()) //handler is used for extra options and custom stuff to use with client
        {

            var request = new HttpRequestMessage(HttpMethod.Post, "https://somesite.com/login");
            request.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); //read this can help with 403 errors
            request.Headers.Add("Accept", "html/txt"); //same, can fix 403

            var CookieJar = new CookieContainer(); //I store cookies from the login request here
            handler.CookieContainer = CookieJar; //bind it to the handler
            var hc = new HttpClient(handler); //create client
            var byteArray = new UTF8Encoding().GetBytes("<user.name@gmail.com>:<password123>"); //creates bytes to send from user:pass pair I guess
            hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); //I have no idea what this does... <copied>

            var formData = new List<KeyValuePair<string, string>>(); //don't know why use this after creating the data in the 2 lines above pair O_o... This code is copied...
            formData.Add(new KeyValuePair<string, string>("username", "user.name@gmail.com")); // don't know
            formData.Add(new KeyValuePair<string, string>("password", "password123")); //...
            formData.Add(new KeyValuePair<string, string>("scope", "all")); //nope....

            request.Content = new FormUrlEncodedContent(formData); //creates data again I guess
            var response = await hc.SendAsync(request); //sends request
            MessageBox.Show(response.ToString()); //debug... I like msgbox
            using (FileStream fileStream = new FileStream("c:\\table.xls", FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //copy the content from response to filestream
                var responseFile = await hc.GetAsync("https://somesite.com/subtab/table.xls");
                await responseFile.Content.CopyToAsync(fileStream); //response is gotten by "hc" which has cookie stored so it should be authed and download right?
            }
        }
    }

代码很糟糕我知道。我猜它也被捣碎了,但抛出的异常太通用了,不包含任何信息。代码现在抛出HttpClient请求中的错误(发送请求时),如果我让它工作(不要问如何)它会给出403

有人可以用它应该看起来的方式写出来/像评论一样工作,所以我终于可以理解如何在HTTP中思考。我想用HttpClient来做这件事,但如果解释得很好,我可以用任何其他方式。谢谢你!

0 个答案:

没有答案