Loading
0

C#HttpWebRequest通过Cookie+Ua模拟自动登录方法

描述:通过HttpWebRequest浏览器cookie+ua的方式自动登录某网站,并将登录成功后的源码输出

1、HttpWebRequest登录方法:


        public static string HttpPost(string Url, string PostData, string cookies,string UA,out string result)
        {
            result = null;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                byte[] data = Encoding.UTF8.GetBytes(PostData);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ServicePoint.ConnectionLimit = 3000;
                request.UserAgent = UA;
                request.ContentLength = data.Length;
                request.Headers.Add("cookie", cookies);
                Stream newStream = request.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                result = reader.ReadToEnd();
                reader.Close();
                request.Abort();
                response.Close();
                return result;
              
             
            }
            catch (Exception ex)
            {
                if (request != null) request.Abort();
                if (response != null) response.Close();
                MessageBox.Show("报错,原因:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return string.Empty;
                
            }
        }


在需要的地方用如下代码调用即可:

HttpPost(Url, PostData, cookies, UA, out result);






 

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:http://blog.tag.gg/showinfo-23-35809-0.html
上一篇:.NET/C#混淆工具/防止反编译工具NETReactor破解版下载
下一篇:C#最小化后缩小到状态栏(任务栏右下角)方法