日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/4/16 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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跳转到制定的网页并自动实现登陆功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。