Exemple #1
0
        /// <summary>
        /// 尝试向登录程序提交用户名和密码信息,获取返回结果
        /// </summary>
        /// <param name="loginUrl">数据提交的目标URL</param>
        /// <param name="method">提交方法</param>
        /// <param name="requestEncoding">请求编码</param>
        /// <param name="responseEncoding">响应编码</param>
        /// <param name="args">随请求一起发送的数据</param>
        /// <returns>WebLoginResult,失败时返回NULL</returns>
        public WebLoginResult Login(string loginUrl, string method, Encoding requestEncoding, Encoding responseEncoding, NameValueCollection args)
        {
            HttpWebRequest request = WebRequest.Create(loginUrl) as HttpWebRequest;

            string agent = this.GetUserAgent();

            request.UserAgent = (null == agent || "" == agent) ? SpiderSetting.DefaultUserAgent : agent;

            //带参数的POST请求
            if (method == "POST" && null != args)
            {
                request.Method      = "POST";
                request.ContentType = String.Format("application/x-www-form-urlencoded;charset={0}", requestEncoding.WebName);
                byte[] data = SpiderBase.GetRequestData(args, null == requestEncoding ? Encoding.Default : requestEncoding);
                request.ContentLength = data.Length;
                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                }
            }

            IWebProxy proxy = this.GetWebProxy();

            if (proxy != null)
            {
                request.Proxy = proxy;
            }

            string referer = this.GetReferer();

            if (null != referer && "" != referer)
            {
                request.Referer = referer;
            }

            HttpWebResponse response = null;

            try
            {
                response = request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            {
                if (Environment.UserInteractive)
                {
                    Console.WriteLine(e);
                }
                return(null);
            }

            if (null == response)
            {
                return(null);
            }

            return(new WebLoginResult(response, responseEncoding));
        }
Exemple #2
0
 private ProcessContext(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
 {
     this.spiderBase = info.GetValue("spiderBase", typeof(SpiderBase)) as SpiderBase;
 }
Exemple #3
0
 /// <summary>
 /// 构造函数
 /// </summary>
 internal ProcessContext(SpiderBase spider)
 {
     this.spiderBase = spider;
 }
 private ProcessContext(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
 {
     this.spiderBase = info.GetValue("spiderBase", typeof(SpiderBase)) as SpiderBase;
 }
 /// <summary>
 /// 构造函数
 /// </summary>
 internal ProcessContext(SpiderBase spider)
 {
     this.spiderBase = spider;
 }