当前位置: 首页 > news >正文

采集/自动登录啊都可以用这两个方法实现 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 = 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;

 

转载于:https://www.cnblogs.com/wawahaha/p/3777137.html

相关文章:

  • windows phone随笔
  • [CLR via C#]11. 事件
  • Welcome to Swift (苹果官方Swift文档初译与注解九)---58~62页(第二章)
  • 高级mask应用
  • 义隆单片机学习笔记之(四) 编程及烧录
  • ASP.NET MD5加密
  • topcoder SRM 618 DIV2 MovingRooksDiv2
  • 微信公众号自定义菜单与回车
  • 协议分析-迅雷镜象服务器下载
  • 一个硬件高手的设计经验分享
  • log4j 将日志记录到数据库
  • 如何用笔记本组建家庭点歌系统
  • 什么是大数据?
  • 动态注册BroadcastReceiver
  • ASP.NET怎么防止多次点击提交按钮重复提交
  • 2017-09-12 前端日报
  • Android开源项目规范总结
  • Android框架之Volley
  • C学习-枚举(九)
  • httpie使用详解
  • If…else
  • js作用域和this的理解
  • npx命令介绍
  • React+TypeScript入门
  • WePY 在小程序性能调优上做出的探究
  • 基于 Babel 的 npm 包最小化设置
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 记录一下第一次使用npm
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 使用putty远程连接linux
  • 首页查询功能的一次实现过程
  • 听说你叫Java(二)–Servlet请求
  • 微信小程序设置上一页数据
  • 小程序button引导用户授权
  • 小而合理的前端理论:rscss和rsjs
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 用 Swift 编写面向协议的视图
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • gunicorn工作原理
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • ​2020 年大前端技术趋势解读
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​力扣解法汇总946-验证栈序列
  • #QT(智能家居界面-界面切换)
  • (八)Spring源码解析:Spring MVC
  • (搬运以学习)flask 上下文的实现
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (一)插入排序
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • ***检测工具之RKHunter AIDE