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跳转到制定的网页并自动实现登陆功能相关推荐

  1. html document.location转到另一个网页,常用跳转代码 打开网页转到其他网址

    打开立马跳转 其他收藏记录 收集了几种比较经典.比较全的跳转代码和加载代码,做自己个人网站的时候经常用到,下面分享给大家: window.location.href='http://www.webuc ...

  2. 怎么实现html图片跳转页面显示内容,0036 如何编写网页迁移跳转功能

    上节课讲解了关于图片的相关知识,这节课来讲解关于如何使用超级链接实现网页之间的跳转功能. 上节课的课后练习 准备做2个新的网页,设计好里面的文字内容和图片. 学哥要来做2个新的网页,一个网页是显示关于 ...

  3. python登陆成功页面跳转_模拟登陆后如何获取跳转的网页?

    爬取虾米音乐时,模拟登陆网页版地址https://login.xiami.com/member/login后,返回的是一串字符(包括status.jumpurl以及账号信息等),但无法获取跳转后的网页 ...

  4. python实现登陆注册跳转_Python模拟登录和登录跳转的参考示例

    # coding:utf-8 import urllib import urllib2 import cookielib from bs4 import beautifulsoup # 设置登录url ...

  5. python登录各种网页示例_Python 通过爬虫实现GitHub网页的模拟登录的示例代码

    1. 实例描述 通过爬虫获取网页的信息时,有时需要登录网页后才可以获取网页中的可用数据,例如获取 GitHub 网页中的注册号码时,就需要先登录账号才能在登录后的页面中看到该信息,如下图所示.那么该如 ...

  6. python登录跳转_Python模拟登录和登录跳转的参考示例

    # coding:utf-8 import urllib import urllib2 import cookielib from bs4 import BeautifulSoup # 设置登录url ...

  7. 关闭、刷新、跳转、离开当前网页前提示

    /*关闭.刷新.跳转.离开当前网页前提示*/ onbeforeunload = function () {return false; };

  8. html 跳转或打开新网页

    html 跳转或打开新网页 <!DOCTYPE html> <html lang="en"> <head><meta charset=&q ...

  9. Winform程序窗体间的跳转

    关于Winform程序窗体间的跳转问题: 对于编程新手来讲,winform程序窗体间的跳转,经常会出现一些异常,或是弹出重复的窗口,如下图 下面给出一种方法,可以很好的避免掉这样的问题: 首先,在主程 ...

最新文章

  1. java B2B2C Springcloud多租户电子商城系统-(七)高可用的分布式配置中心(Spring Cloud Config)...
  2. 《c++ templates》学习笔记(9)——第十二章 特化与重载
  3. Java设计模式----策略模式(Strategy)
  4. 手持巡检仪_专业的三防产品设计-电力巡检仪设计
  5. simpla是基于laravel5的php,一个基于laravel5.1的后台
  6. 内存管理(注意事项和重点)
  7. php查找之二分查找
  8. 线程的sleep()方法和yield()方法有什么区别?
  9. 常用模块之hashlib,subprocess,logging,re,collections
  10. 367.有效的完全平方数   (力扣leetcode) 博主可答疑该问题
  11. instagram下载_Instagram Raider可让您轻松下载Instagram图像和视频
  12. postgresql源码学习(十)—— 常规锁③-主锁表与进程锁表的初始化与查询
  13. 计算机什么快捷键是睡眠,电脑睡眠快捷键(ctrl加哪个键是睡眠)
  14. windows局域网的一个经典的入侵方法
  15. 高效程序员秘籍(5):让EyeDefender提醒你注意休息,保护视力
  16. 样本调试之 loadlibrary 报错1114
  17. 教你从零做起谷歌Adsense。开户,过审核,过pin码达到稳定收益
  18. 小白必看!数据库自学入门教程,免费的SQL认证课程
  19. linux get与put,科技常识:Linux ftp 命令行中下载文件get与上传文件put的命令应用详解...
  20. 川希:不打造个人IP,你完蛋的速度将会超乎你想象!

热门文章

  1. 使用Charles抓包
  2. [雪峰磁针石博客]2018最佳12个开源或免费web服务器和客户端性能测试工具
  3. 从零开始玩人工智能-人脸 API-03
  4. C#获取文件的MD5码
  5. 学习AI可能不需要那么多数学知识:20小时进阶计划
  6. Docker 从Docker Hub 拉取镜像慢怎么办?
  7. ArcGIS Engine开发前基础知识(2)
  8. mongodb的读写分离
  9. 自动化测试如何解决验证码的问题
  10. hashCode之一--两个对象值相同,有相同的hash code