采集/自动登录啊都可以用这两个方法实现 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 上的虚拟机,可以设置定时关机操作,并在适当的时间把它开机,这 ...
随机推荐
- Asp.Net 之 抓取网页内容
一.获取网页内容——html ASP.NET 中抓取网页内容是非常方便的,而其中更是解决了 ASP 中困扰我们的编码问题. 需要三个类:WebRequest.WebResponse.StreamRea ...
- jboss部署出现jboss.naming.context.java.rmi找不到错误
最近,在机器人程序中使用jmx,准备做个远程调用,客户端是web,部署在jboss上,本地测试的都好好的,发到预发布上就是不行, 错误描述: Failed to retrieve RMIServer ...
- android 代码设置、打开wifi热点及热点的连接
用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi热点,然后另一台便连接这个热点再进行传输.快牙传输速度惊人应该跟它的这种机制有关系吧.不知道它的搜索机制是怎样的,但我想应该可以通 ...
- 如何使用CSS实现小三角形效果
如何使用CSS实现小三角形效果:建议:尽可能的手写代码,可以有效的提高学习效率和深度.在众多的网页效果中,都有小三角形效果的应用,能够增加特定应用的美观度,下面就给出一段实例代码,里面介绍了两种实现小 ...
- JS中的嵌套作用域
在JS中仅仅区分全局变量和局部变量还不够,实际上,变量作用域可以有任意层级(嵌套).其他函数内部定义的函数可以调用父函数的局部变量,而内部函数里定义的函数则不仅可以调用父函数的局部变量,还可以调用祖父 ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, Properties
The properties configuration element can be used to externalize the configuration values into a prop ...
- Linux 命令 - mkdir: 创建目录
命令格式 mkdir [OPTION]... DIRECTORY... 命令参数 -m, --mode=MODE 设置文件的模式,类似于 chmod 命令. -p, --parents 需要时创建指定 ...
- Ajax-jQuery实现
load方法: 对上一篇中的javascript原生实现(数据格式为html),用jqury的post方法进行改造: 下面这句中“h2 a”,实现了对返回内容的选择: $.get()\$.post ...
- JavaScript之可运行按钮
看到好多大神都写了像这种在页面"可运行"的Javascript脚本,感觉很好奇,所以我今天也试着写了一个. 自从有了这个"可运行"按钮,好多代码就再也不以图片的 ...
- Servlet之创建与配置
上篇已将介绍完了,下面来实践操作走一个: 首先在名为"com.caiduping"的包中创一个MyFilter的对象: 1 package com.caiduping; 2 3 i ...