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






 
技术小学生微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://blog.tag.gg/showinfo-23-35809-0.html
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
上一篇:.NET/C#混淆工具/防止反编译工具NETReactor破解版下载
下一篇:C#最小化后缩小到状态栏(任务栏右下角)方法