/// <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的更多相关文章

  1. 自动化web前端测试,自动登录网站.目前发现最靠谱的方法是imacros

    imacros免费版 登录宏代码的示例: //首先登出URL GOTO=http://yoursite/logout.html//打开登录页面URL GOTO=http://yoursite/logi ...

  2. 基于localStorge开发登录模块的记住密码与自动登录

    前沿||我是乐于分享,善于交流的鸟窝 先做写一篇关于登录模块中记住密码与自动登录的模块.鸟窝微信:jkxx123321 关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个 ...

  3. Linux expect实现自动登录

    expect expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令.当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令.但当不能使用密钥验证的时候,我们就没有办法 ...

  4. Golddata如何采集需要登录/会话的数据?

    概要 本文将介绍使用GoldData半自动登录功能,来采集需要登录网站的数据.GoldData半自动登录功能,就是指通过脚本来执行登录,如果需要验证码或者其它内容需要人工输入时,可以通过收发邮件来执行 ...

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

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

  6. 简单的行为控制管理方法,自动登录,session定时验证码过期

    代码很简单 实现的方式很多,用cookies 用static 变量 file文件缓存 等等 比如 //简单行为管理,如果请求此方法次数多于5次,就显示验证码 吧当前方法的name传进来,有效时间是5分 ...

  7. java 自动登录代码

    javaBean的代码    package bean;    import java.io.Serializable;    public class Admin implements Serial ...

  8. linux expect详解(ssh自动登录)

    shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh root@192.168.22.194 expect "*password:&quo ...

  9. 使用 PowerShell 自动登录Azure

    PowerShell 是管理 Azure 的最好方式之一,通过使用 PowerShell 脚本可以把很多的工作自动化.比如对于 Azure 上的虚拟机,可以设置定时关机操作,并在适当的时间把它开机,这 ...

随机推荐

  1. js中将字符串转换成json的方式

    1.eval 方式解析,实际中用的还是比较少 function evalJson(str){ var json = eval('(' + str + ')'); return json; } 2.使用 ...

  2. [Visual Studio] 开启Visual Studio 2012通过右键菜单创建单元测试(Unit Test)

    Visual Studio 2012可以说是迄今为止微软VS开发工具中用户体验最好的产品,无论是速度还是体验以及功能,都非常出色,但是,使用了一段时间后发现有一个之前版本VS都有的功能却在Visual ...

  3. uva 133 The Dole Queue 双向约瑟夫环 模拟实现

    双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...

  4. python(3)-内置函数

    >>> abs(-1) #绝对值,小数也可以,不能是其它字符 1 >>> all([1,2,3,4,5,6,7]) #如果传入的列表所有元素都为真,则True Tr ...

  5. CF Fox And Two Dots (DFS)

    Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. Android混淆打包配置总结

    Android打包失败出现Proguard returned with error code 1. See console的错误 这个问题是由于代码混淆引起的,找不到引用包. 只需在你的proguar ...

  7. 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10(转)

     在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html&g ...

  8. Angular 2.0 从0到1 (六)

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  9. PHP,单双引号的区别‘“”“”’

    一.转义 单引号:只转义\'和\\,其他不转义 双引号:都转义 二.变量解析 单引号:无法解析变量(原样输出) 双引号:可以解析变量 三.速度 单引号:更快 双引号:要分析是否有变量,转义内容更多 结 ...

  10. memcached缓存机制+微软缓存机制使用详解

    1. why Memcached 1.1   一台web服务器上,iis接收的请求数是有限的,当访问量超大的时候,网站访问就会遇到瓶颈了,处理方式就是运用多了服务器把请求数分流(集群),对外公布的就一 ...