采集/自动登录啊都可以用这两个方法实现 asp.net
2014-06-09 04:22
771 查看
/// <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 = 6000; //超时时间 request.Method = "get"; //get方式提交 request.ContentType = "application/x-www-form-urlencoded;charset=" + Encoding; request.AllowAutoRedirect = true; //是否跟随重定向 request.MaximumAutomaticRedirections = 10; //重定向最大数 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 > 0) { parmsStr = parmsStr.Substring(1, parmsStr.Length - 1); //把第一个"&"删了 } } byte[] postBytes = myEncoding.GetBytes(parmsStr); request.Timeout = 60000; //超时时间 request.Method = "post"; //采用post方法提交 request.ContentType = "application/x-www-form-urlencoded;charset=" + myEncoding; request.ContentLength = postBytes.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(postBytes, 0, 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
- asp.net利用cookie保存用户密码实现自动登录的方法
- asp.net利用cookie保存用户密码实现自动登录的方法
- asp.net cookie 实现 记录用户登录状态,下次自动登录
- ASP.NET页面刷新:自动刷新页面的实现方法总结
- ASP.NET jQuery 食谱25 (使用jQuery UI的Autocomplete方法实现文本框的自动搜索填充功能)
- 在 ASP.NET 中实现不同角色的用户使用不同登录界面的方法
- 在 ASP.NET 中实现不同角色的用户使用不同登录界面的方法
- ASP.NET jQuery 随笔 使用jQuery UI的Autocomplete方法实现文本框的自动搜索填充功能
- asp.net c#采集需要登录页面的实现原理及代码
- 建立一个方法的attribute,可以放在任意方法上,可以自动记录方法出错时的信息,就不用写try 。。cacth. 【注意】 不是在asp.net MVC下,是在普通三层结构下写的的特性。
- Asp.net项目同一个用户后登录把前面登录的踢下线的两种实现方法
- 一起谈.NET技术,在 ASP.NET 中实现不同角色的用户使用不同登录界面的方法
- asp.net中倒计时自动跳转页面的实现方法(使用javascript)
- 【转】 asp.net从视频文件中抓取一桢并生成图像文件的方法 实现多语言本地化应用程序 自动返回上次请求页面
- 在 ASP.NET 中实现不同角色的用户使用不同登录界面的方法
- asp.net forms 身份验证方式下跨域登录信息共享的实现方法
- WinServiceTask 任务调度,也可以用于ASP.NET实现自动服务
- 在ASP.NET登录页面中如何实现文本框焦点自动跳转及通过回车键提交表单