/// <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. [PHP] Eclipse开发PHP环境配置

    首先准备好软件: 1. Apache,到这里找个最新版本 2. PHP,到这里下载 3. Eclipse IDE for Java EE Developers,到这里下载 4. DLTK Core F ...

  2. Android进阶笔记06:Android 实现扫描二维码实现网页登录

    一. 扫描二维码登录的实现机制: 详细流程图: (1)PC端打开网页(显示出二维码),这时候会保存对应的randnumber(比如:12345678). (2)Android客户端扫码登录,Andro ...

  3. iOS之上线被拒

    前言 感谢您付出宝贵的才华与时间来开发iOS应用程程序.从职业与报酬的角度而言,这对于成千上万的开发员来说一直都是一项值得投入的事业,我们希望帮助您加入这个成功的组织.我们发布了<App Sto ...

  4. Android网络请求与解析

    1.Volley和Gson结合使用——Volley适用于小型数据,多次的请求,使用Gson解析时,服务器数据的键值不能包含常用的标识符如:class.....等,这些就需要与服务端小伙伴商量 这样也可 ...

  5. Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary

    NSDictionary(字典),NSDictionary类似于 .net中的parameter,l类似于java中的map. 通过唯一的key找到对应的值,一个key只能对应一个只,而多个key可以 ...

  6. LeetCode 345

    Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels ...

  7. Dubbo服务调用的动态代理和负载均衡

    Dubbo服务调用的动态代理及负载均衡源码解析请参见:http://manzhizhen.iteye.com/blog/2314514

  8. 【转】测试用例设计——WEB通用测试用例

    现在项目做完了,我觉得还是有必要总结一下,学习到的内容.毕竟有总结才能有提高嘛!总结一下通用的东西,不管什么项目基本都可能会遇到,有写地方也有重复的或者有的是按照个人的习惯来总结的不一定都对,有不对的 ...

  9. 浅谈call和apply的联系&区别&应用匹配

    call和apply的联系和区别在之前查过资料了解了一番,昨天晚上睡不着觉忽然想到了这个问题,发现对于他们的联系和区别理解的还是很模糊.看来还是欠缺整理,知识没有连贯起来.反思一二,详情如下: 1作用 ...

  10. Java Concurrency - ReentrantLock

    ReentrantLock 是可重入的互斥锁,它具有与使用 synchronized 方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大. ReentrantLock 将由最近成功获 ...