欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

winform模拟登陆网页_winform跳转到制定的网页并自动实现登陆功能

发布时间:2025/4/16 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 winform模拟登陆网页_winform跳转到制定的网页并自动实现登陆功能 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

using System;

using System.Collections.Generic;

using System.Text;

using using namespace Czt.Web

{

//实现网站登录类/// public class Post

{

//网站Cookies/// private string _cookieHeader = string.Empty;

public string CookieHeader

{

get

{

return _cookieHeader;

}

set

{

_cookieHeader = value;

}

}

//网站编码/// private string _code = string.Empty;

public string Code

{

get { return _code; }

set { _code = value; }

}

private string _pageContent = string.Empty;

public string PageContent

{

get { return _pageContent; }

set { _pageContent = value; }

}

private Dictionary _para = new Dictionary();

public Dictionary Para

{

get { return _para; }

set { _para = value; }

}

/**/

//功能描述:模拟登录页面,提交登录数据进行登录,并记录Header中的cookie//登录数据提交的页面地址///用户登录数据///引用地址///网站编码///可以返回页面内容或不返回 public string PostData(string strURL, string strArgs, string strReferer, string code, string method)

{

return PostData(strURL, strArgs, strReferer, code, method, string.Empty);

}

public string PostData(string strURL, string strArgs, string strReferer, string code, string method, string contentType)

{

try

{

string strResult = "";

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);

myHttpWebRequest.AllowAutoRedirect = true;

myHttpWebRequest.KeepAlive = true;

myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";

myHttpWebRequest.Referer = strReferer;

myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";

if (string.IsNullOrEmpty(contentType))

{

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

}

else

{

myHttpWebRequest.ContentType = "contentType";

}

myHttpWebRequest.Method = method;

myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");

if (myHttpWebRequest.CookieContainer == null)

{

myHttpWebRequest.CookieContainer = new CookieContainer();

}

if (this.CookieHeader.Length > 0)

{

myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader);

myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader);

}

byte[] postData = Encoding.GetEncoding(code).GetBytes(strArgs);

myHttpWebRequest.ContentLength = postData.Length;

PostStream = myHttpWebRequest.GetRequestStream();

PostStream.Write(postData, 0, postData.Length);

PostStream.Close();

HttpWebResponse response = null;

sr = null;

response = (HttpWebResponse)myHttpWebRequest.GetResponse();

if (myHttpWebRequest.CookieContainer != null)

{

this.CookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL));

}

sr = new (response.GetResponseStream(), Encoding.GetEncoding(code)); utf-8 strResult = sr.ReadToEnd();

sr.Close();

response.Close();

return strResult;

}

catch (Exception ex)

{

Utilities.Document.Create("C:\\error.log", strArgs, true, Encoding.UTF8);

}

return string.Empty;

}

/**/

//功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容//获取网站的某页面的地址///引用的地址///返回页面内容 public string GetPage(string strURL, string strReferer, string code)

{

return GetPage(strURL, strReferer,code,string.Empty);

}

public string GetPage(string strURL, string strReferer,string code,string contentType)

{

string strResult = "";

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);

myHttpWebRequest.AllowAutoRedirect = true;

myHttpWebRequest.KeepAlive = false;

myHttpWebRequest.Accept = "*/*";

myHttpWebRequest.Referer = strReferer;

myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");

myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";

if (string.IsNullOrEmpty(contentType))

{

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

}

else

{

myHttpWebRequest.ContentType = contentType;

}

myHttpWebRequest.Method = "GET";

if (myHttpWebRequest.CookieContainer == null)

{

myHttpWebRequest.CookieContainer = new CookieContainer();

}

if (this.CookieHeader.Length > 0)

{

myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader);

myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader);

}

HttpWebResponse response = null;

sr = null;

response = (HttpWebResponse)myHttpWebRequest.GetResponse();

Stream streamReceive;

string gzip = response.ContentEncoding;

if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")

{

streamReceive = response.GetResponseStream();

}

else

{

streamReceive = new (response.GetResponseStream(), );

}

sr = new (streamReceive, Encoding.GetEncoding(code));

if (response.ContentLength > 1)

{

strResult = sr.ReadToEnd();

}

else

{

char[] buffer=new char[256];

int count = 0;

StringBuilder sb = new StringBuilder();

while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)

{

sb.Append(new string(buffer));

}

strResult = sb.ToString();

}

sr.Close();

response.Close();

return strResult;

}

}

}

总结

以上是生活随笔为你收集整理的winform模拟登陆网页_winform跳转到制定的网页并自动实现登陆功能的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。