采集/自动登录啊都可以用这两个方法实现 asp.net
/// <summary>
/// 通过get方式发送xmlHttp请求,并获得响应数据
/// </summary>
/// <param name="Url">URL地址,参数直接写到后面,如:http://www.baidu.com/index.asp?id=7</param>
/// <param name="Encoding">请求和返回数据采用的编码方式,如 "gb2312" ,"utf-8"</param>
/// <returns></returns>
public static string SendXmlHttpWithGet(string Url, string Encoding, CookieContainer cookie)
{
HttpWebRequest request;// = (HttpWebRequest)WebRequest.Create(Url);
string ResponseHtml = "";
try
{
request = (HttpWebRequest)WebRequest.Create(Url);
request.CookieContainer = cookie;
request.KeepAlive = false; //是否建立持久连接
request.Timeout = ; //超时时间
request.Method = "get"; //get方式提交
request.ContentType = "application/x-www-form-urlencoded;charset=" + Encoding;
request.AllowAutoRedirect = true; //是否跟随重定向
request.MaximumAutomaticRedirections = ; //重定向最大数
request.AllowWriteStreamBuffering = false; //是否对发送数据进行缓冲处理
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; //进行身份验证 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获得响应
Stream responseStream = response.GetResponseStream(); //获得响应流
StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding(Encoding)); //读取字节的方式读取流 ResponseHtml = readStream.ReadToEnd(); //读完流
responseStream.Close(); //关闭响应流
readStream.Close();//关闭字节流
}
catch (Exception ex)
{
ResponseHtml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><data><success value=\"0\" msg=\"登录失败!" + ex.Message.ToString() + "\" /></data>";
}
finally { }
return ResponseHtml;
} /// <summary>
/// 通过post方式发送xmlHttp请求,并获得响应数据
/// </summary>
/// <param name="url">URL地址</param>
/// <param name="parms">需要处理的参数键值对</param>
/// <param name="encoding">请求和返回数据采用的编码方式,如 "gb2312" ,"utf-8"</param>
/// <returns></returns>
public static string SendXmlHttpWithPost(string url, Hashtable parms, string encoding, CookieContainer cookie)
{
HttpWebRequest request ;//= (HttpWebRequest)WebRequest.Create(url);
string ResponseHtml = "";
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookie;
////传参数
if (encoding == null) { encoding = "utf-8"; }
Encoding myEncoding = Encoding.GetEncoding(encoding); //指定编码 string parmsStr = "";
if (parms != null)
{
foreach (DictionaryEntry item in parms)
{
parmsStr += "&" + HttpUtility.UrlEncode(item.Key.ToString(), myEncoding);
parmsStr += "=" + HttpUtility.UrlEncode(item.Value.ToString(), myEncoding);
}
if (parmsStr.Length > )
{
parmsStr = parmsStr.Substring(, parmsStr.Length - ); //把第一个"&"删了
}
}
byte[] postBytes = myEncoding.GetBytes(parmsStr); request.Timeout = ; //超时时间
request.Method = "post"; //采用post方法提交
request.ContentType = "application/x-www-form-urlencoded;charset=" + myEncoding;
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, , postBytes.Length); //把参数写入请求流
requestStream.Close(); //获取响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取响应
Stream responseStream = response.GetResponseStream();//获取响应流
StreamReader readStream = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); //采用StreamReader用指定编码读取响应流 ResponseHtml = readStream.ReadToEnd(); //读完流
responseStream.Close(); //关闭响应流
readStream.Close();//关闭字节流
}
catch (Exception ex)
{
ResponseHtml= "<?xml version=\"1.0\" encoding=\"utf-8\" ?><data><success value=\"0\" msg=\"登录失败!" + ex.Message.ToString() + "\" /></data>";
}
return ResponseHtml;
采集/自动登录啊都可以用这两个方法实现 asp.net的更多相关文章
- 自动化web前端测试,自动登录网站.目前发现最靠谱的方法是imacros
imacros免费版 登录宏代码的示例: //首先登出URL GOTO=http://yoursite/logout.html//打开登录页面URL GOTO=http://yoursite/logi ...
- 基于localStorge开发登录模块的记住密码与自动登录
前沿||我是乐于分享,善于交流的鸟窝 先做写一篇关于登录模块中记住密码与自动登录的模块.鸟窝微信:jkxx123321 关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个 ...
- Linux expect实现自动登录
expect expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令.当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令.但当不能使用密钥验证的时候,我们就没有办法 ...
- Golddata如何采集需要登录/会话的数据?
概要 本文将介绍使用GoldData半自动登录功能,来采集需要登录网站的数据.GoldData半自动登录功能,就是指通过脚本来执行登录,如果需要验证码或者其它内容需要人工输入时,可以通过收发邮件来执行 ...
- 利用activeX控件在网页里自动登录WIN2003远程桌面并实时控制
首先要自己配置并打开受控端的WEB远程桌面服务,这个在“添加/删除windows组件”里有,我只在windows 2003 server里试过,没试过XP.下面我们在客户端安装微软提供的远程桌面客户端 ...
- 简单的行为控制管理方法,自动登录,session定时验证码过期
代码很简单 实现的方式很多,用cookies 用static 变量 file文件缓存 等等 比如 //简单行为管理,如果请求此方法次数多于5次,就显示验证码 吧当前方法的name传进来,有效时间是5分 ...
- java 自动登录代码
javaBean的代码 package bean; import java.io.Serializable; public class Admin implements Serial ...
- linux expect详解(ssh自动登录)
shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh root@192.168.22.194 expect "*password:&quo ...
- 使用 PowerShell 自动登录Azure
PowerShell 是管理 Azure 的最好方式之一,通过使用 PowerShell 脚本可以把很多的工作自动化.比如对于 Azure 上的虚拟机,可以设置定时关机操作,并在适当的时间把它开机,这 ...
随机推荐
- CentOS中TFTP配置
转载:http://www.centoscn.com/image-text/config/2013/1105/2062.html TFTP是用来下载远程文件的最简单网络协议,它其于UDP协议而实现 1 ...
- [PHP] Eclipse开发PHP环境配置
首先准备好软件: 1. Apache,到这里找个最新版本 2. PHP,到这里下载 3. Eclipse IDE for Java EE Developers,到这里下载 4. DLTK Core F ...
- Python学习笔记 第二课 循环
>>> movies=["The Holy Grail", 1975, "The Life of Brian", 1979, "Th ...
- Objective-C基础笔记一
这里开始了我OC旅程 花了8天的时间粗略的学习了新知识Objective-C(简称OC),虽然只是学习了其中的基础部分,但经过这一周的学习也算是入门了.对面向对象的封装.继承.多态以及其中所包含的方法 ...
- SQL Server :事务和锁
1.事务 事务概念:全部执行或全部不执行的一条或者多条语句的组合 例子说明:到银行里转账,将一个账户(Tom)里的100元钱转到另一个账户(Jake) update table money=money ...
- 关于win7系统中所有exe文件都被以word方式打开的解决方法
手残一刻,电脑桌面所有的软件快捷方式都变成了word的打开方式,鼠标右键选中某exe文件也没打开方式那个选项, 第一次尝试: 在控制面板——默认程序中修改默认打开方式,但是没有找到解决方法
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 关于JDK中的集合总结(二)
1.2版本的JDK才出现的java集合框架. 下面介绍说一下Vector的一些特点. import java.util.Enumeration; import java.util.Iterator; ...
- MakeFile 文件详解
GNU的make工作时的执行步骤入下:(想来其它的make也是类似) 1.读入所有的Makefile. 2.读入被include的其它Makefile. 3.初始化文件中 ...
- Windows 7 IE11 F12 不能正常使用
打开任意网站,按下F12,或者右键鼠标,按L键.出现上面的图的情况!解决办法如下:需要安装下面的补丁(KB3008923) 32位系统:http://www.microsoft.com/en-us/d ...