打开网页,登录然后下载文件 [英] Open web page, login and then download file

查看:157
本文介绍了打开网页,登录然后下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须打开一个网页(https),输入用户名和密码,点击一些链接进入所需的页面,然后将所有文件(pdf / txt)下载到我们内部网络的共享文件夹中。



我想用Windows应用程序/服务这样做,它将在一天内运行一次。最好的方法是什么?

I have to open a web page (https), enter username and password, click some links to get to a desired page and then download all files (pdf/txt) to a shared folder on our internal network.

I''d like to do this with windows application/Service which would run once in a day. What is the best way to do this?

推荐答案

请参阅下面的示例,这将通过登录将任何源代码附加到CodeProject的任何文章中下载。



See the example below, this will download any sourcecode attachwed to any article from CodeProject by logging in.

private static void Download(string email, string password, string destination, string downloadUri)
{

    using (CookiesAwareWebClient client = new CookiesAwareWebClient())
    {
        NameValueCollection values = new NameValueCollection();
        values.Add("Email",email );
        values.Add("Password", password);
        values.Add("x", "10"); 
        values.Add("y", "10"); 
        values.Add("login", "login");

        // We authenticate first
        client.UploadValues("https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f", values);

        // Now we can download

        string modifieduri = "http://www.codeproject.com/script/articles/download.aspx?file=" + downloadUri.Replace("http://www.codeproject.com","") + "&rp=";


        string filename = System.IO.Path.GetFileName(downloadUri);

        string filepathname = System.IO.Path.Combine(destination, filename);

        client.DownloadFile(modifieduri, filepathname);
    }
}







代码中使用的类(CookiesAwareWebClient)上述:




The class used(CookiesAwareWebClient) in the code above:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace CodeProjectDownload
{
    public class CookiesAwareWebClient : WebClient
    {
        public CookieContainer CookieContainer;
        public CookieContainer MyProperty
        {
            get { return CookieContainer; }
            set { CookieContainer = value; }
        }
        public CookiesAwareWebClient()
        {
            CookieContainer = new CookieContainer();
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            ((HttpWebRequest)request).CookieContainer = CookieContainer;
            return request;
        }
    } 
}





另一种选择(通用):



Another Option(Generic):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace IndianaMedicAidAccess
{
    public class HTTHelper
    {
        private static HttpWebResponse POST(string postData, string url, string referer, string cookie)
        {
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                request.KeepAlive = true;
                request.AllowAutoRedirect = false;
                request.Accept = "*/*";
                request.ContentType = "application/x-www-form-urlencoded";
                if (!string.IsNullOrEmpty(cookie))
                    request.Headers.Add(HttpRequestHeader.Cookie, cookie);
                if (!string.IsNullOrEmpty(referer))
                    request.Referer = referer;
                request.ContentLength = byteArray.Length;
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
                //   request.Proxy = null;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                try
                {
                    return (HttpWebResponse)request.GetResponse();
                }
                catch
                {
                    return (HttpWebResponse)null;
                }
            }
            catch
            {
                return (HttpWebResponse)null;
            }
        }
        public static string ProcessLogin(string postData, string LoginUrl, string Referer)
        {
            string LoginStatus = "Unsucessful";
            string uri = string.Empty;
            string cookie = string.Empty;
            HttpWebResponse response = POST(postData, LoginUrl, Referer, "");
            if (response == null)
            {
                return LoginStatus;
            }

            WebHeaderCollection headers = response.Headers;

            if ((response.StatusCode == HttpStatusCode.Found) ||
                    (response.StatusCode == HttpStatusCode.Redirect) ||
                    (response.StatusCode == HttpStatusCode.Moved) ||
                    (response.StatusCode == HttpStatusCode.MovedPermanently))
            {


                if (headers["Set-Cookie"] != null)
                {

                    string cookies = headers["Set-Cookie"];
                    List<string> fieldList = cookies.Split(',').ToList();
                    response.Close();
                    foreach (string str in fieldList)
                    {
                        try
                        {
                            if (str.IndexOf("=") > -1)
                            {
                                string mystr = str.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)[0];
                                cookie += mystr + ";";
                            }
                        }
                        catch { }
                    }
                    cookie = cookie.LastIndexOf(";") == cookie.Length - 1 ? cookie.Substring(0, cookie.Length - 1) : cookie;
                    if (string.IsNullOrEmpty(cookie))
                    {
                        String[] fields = Regex.Split(cookies, ";\\s*");
                        cookie = fields[0];
                       
                    }
                }
            }
            return cookie;

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string LoginUrl = "https://interchange.indianamedicaid.com/Administrative/LogonValidate.aspx";
            string Referer="https://interchange.indianamedicaid.com/Administrative/logon.aspx?Logon=Y";
            string _userName = "youruserId"; // please change
            string _password = "passwd"; // please change
            string downloadUri = "https://xfile.indianamedicaid.com/......."; // please change
            string destination = Path.Combine("c:\\Somefolder", "FileName.ext");  //please change
            string postData = String.Format("logonid={1}&logonpswd={2}&Log On=Log On", _userName, _password);
            
            string cookie = HTTHelper.ProcessLogin(postData, LoginUrl, Referer);

            WebClient wb = new WebClient();
            wb.Headers.Add(HttpRequestHeader.Cookie, cookie);
            wb.DownloadFile(downloadUri, destination);
        }
    }
}


这篇关于打开网页,登录然后下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆