需求:客户的数据同时存在在另外一个不可控的系统中,需要和当前系统同步。

思路:自动登录另外一个系统,然后抓取数据,同步到本系统中。

技术点:模拟用户登录;保存登录状态;抓取数据

/// <summary>
/// visit the target url
/// </summary>
/// <param name="targetURL"></param>
/// <param name="cc">this is for keeping cookies and sessions</param>
/// <param name="param">this is the data need post inside form</param>
/// <returns>html page</returns>
public static string PostAndGetHTML(string targetURL,CookieContainer cc,Hashtable param)
{
//prepare the submit data
string formData = "";
foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString() + "&";
}
if (formData.Length > )
formData = formData.Substring(, formData.Length - ); //remove last '&' ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(formData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)"; Stream newStream = request.GetRequestStream();
newStream.Write(data, , data.Length); newStream.Close(); request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string result = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
return result;
} public static DataTable ConvertToDT(DataTable dt, string tableHTML)
{ int lastTD = tableHTML.ToLower().LastIndexOf("</td>");
int firstRow = tableHTML.ToLower().IndexOf("<tr") + ;//after ""<tr
int index = tableHTML.ToLower().IndexOf("<tr", firstRow) + ;//after ""<tr
while (index < lastTD)
{
DataRow dr = dt.NewRow();
for (int i = ; i < dt.Columns.Count; i++)
{
string value = "";
int startTD = tableHTML.ToLower().IndexOf("<td", index) + ;//after "<td"
int endTD = tableHTML.ToLower().IndexOf("</td>", startTD);
if (endTD < )
break;
string tdStr = tableHTML.Substring(startTD, endTD - startTD); //remove <> and others
tdStr = tdStr.Replace("&nbsp;", "").Replace("\t", "").Replace("\r", "");
string[] v = tdStr.Split('<', '>');
for (int j = ; j < v.Length; j++)
{
j++;
if (v[j].Trim() != "")
{
value = v[j].Trim();
break;
}
}
//
dr[i] = value;
index = endTD;
}
dt.Rows.Add(dr); }
return dt;
}

这一个是调用的例子:先登录,在查询。 实际中这个逻辑可能有很多步骤

CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie
Hashtable param = new Hashtable();//this is for keep post data. string urlLogin = "http://demo.server//login.asp";
//do find the elementId that needed. check the source of login page can get this information
param.Add("User", "xxx");
param.Add("Password", "xxxx");
string result =GrabHelper.PostAndGetHTML(urlLogin, cc, param);
//check result, whether login success //if login success, goto the target url, and input some value.
string url2 = " http://demo.server/query.asp?id=1";// need change. special logic
param.Clear();
//param.Add("SearchAreaId","JobId")
result = GrabHelper.PostAndGetHTML(url2, cc, new Hashtable());
//ConvertToDT the html or do something others

C# 自动登录网页,浏览页面【转载】的更多相关文章

  1. python 自动登录网页

    语言:python 浏览器:chrome 工具:chrome控制台 #!/usr/bin/python # coding: GBK import urllib,urllib2,httplib,cook ...

  2. 获取全局上下文(getApplicationContext)_创建Shared Preference工具类_实现自动登录

    获取全局上下文(getApplicationContext)_创建Shared Preference工具类_实现自动登录 ===========================获取全局上下文(getA ...

  3. C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录

    最近学习C#网络编程中,想实现网页自动登录并提交GET/POST信息,再实现循环登录不断发送报文给服务器,服务器发送消息给客户端记录能登录的账户和密码,做到后面实现绕过验证码.动态抓取登录位置等,但由 ...

  4. 类似818tu.co微信小说分销系统设计之多公众号网页授权自动登录源码

    /** 转载请保留原地址以及版权声明,请勿恶意修改 *  作者:杨浩瑞  QQ:1420213383  独立博客:http://www.yxxrui.cn * [后台]http://xiaoshuo. ...

  5. 类似818tu.c微信小说分销系统设计之多公众号网页授权自动登录源码分享

    /** 转载请保留原地址以及版权声明,请勿恶意修改 *  作者:杨浩瑞  QQ:1420213383  独立博客:http://www.yxxrui.cn * [后台]http://xiaoshuo. ...

  6. 利用activeX控件在网页里自动登录WIN2003远程桌面并实时控制

    首先要自己配置并打开受控端的WEB远程桌面服务,这个在“添加/删除windows组件”里有,我只在windows 2003 server里试过,没试过XP.下面我们在客户端安装微软提供的远程桌面客户端 ...

  7. C#网页自动登录和提交POST信息的多种方法(转)

    网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser.WebClient.HttpWebRequest这三 ...

  8. Laravel 登录后跳转回登录前浏览的页面

    一.经过 Auth 中间件检查后跳转至登录页面 也就是没有通过 auth 中间件的认证检查,被 auth 中间件拦截后跳转至登录页面.这种情况下,Laravel 默认会在用户登录成功后自动跳转回登录前 ...

  9. 苹果手机Safari无痕浏览模式下系统登录成功但是页面不跳转

    昨天下午,测试提了一个bug,问题是:在苹果手机Safari无痕浏览模式下系统登录成功但是页面不跳转. 思前想后找了半天没思路,后来经过同事的点拨,说可能是禁用了cookie之类的,反正我也没思路就顺 ...

随机推荐

  1. PHP设计模式之:策略模式

    <?phpabstract class Strategy{    public abstract function AlgorithmInterface();} class ConcreteSt ...

  2. PHP创建桌面快捷方式实例

    要利用php创建桌面快捷方式我们需要借助于header,InternetShortcut及一些我看不懂的代码. 方法:新建一个php文件,然后把下面的代码扔进去,保存为比如shortcut.php,放 ...

  3. Shortcut Collapse project or projects in the Solution Explorer Microsoft Visual Studio 2008

    The standard windows keyboard shortcuts for expanding and collapsing treeviews are: Numeric Keypad * ...

  4. 编程思想—面向切面编程(AOP)

    谈到面向切面的编程,我们很容易关联到面向对象编程(OOP).个人对这两种编程方式的解释为:两种编程思想只是站在编程的角度问题. OOP注重的是对象,怎么对对象行为和方法的抽象.如何封装一个具有完整属性 ...

  5. MyEclipse10

    1.配置tomcat Windows->Preferences->My Eclipse->Servers->Tomcat,对于64位操作系统而言,Tomcat home dir ...

  6. 线性表之顺序存储结构(C语言动态数组实现)

    线性表的定义:N个数据元素的有限序列 线性表从存储结构上分为:顺序存储结构(数组)和 链式存储结构(链表) 顺序存储结构:是用一段连续的内存空间存储表中的数据 L=(a1,a2,a3....an) 链 ...

  7. [BZOJ 3564] [SHOI2014] 信号增幅仪 【最小圆覆盖】

    题目链接:BZOJ - 3564 题目分析 求最小椭圆覆盖,题目给定了椭圆的长轴与 x 轴正方向的夹角,给定了椭圆长轴与短轴的比值. 那么先将所有点旋转一个角度,使椭圆长轴与 x 轴平行,再将所有点的 ...

  8. 【JavsScript】XMLHttpRequest2的进步之处

    本文参考自:XMLHttpRequest2 新技巧 (重点保留demo,方便自己日后查阅) HTML5是现在web开发中的热点,虽然关于web app和local app一直有争论,但是从技术学习的角 ...

  9. Web-Scale-IT 到底是啥?

    Gartner 对 2015 年 10 大 IT 趋势的预测中有一个词条为:Web Scale IT.我们跟随 Matthias Ankli 来了解一下究竟什么是 Web Scale IT.本文译自 ...

  10. KMP字符串模式匹配详解(转)

    来自CSDN     A_B_C_ABC 网友 KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n);KMP匹配算法.可以证明它的时间复杂度 ...