• webclient类学习


    (HttpWebRequest模拟请求登录):当一些硬件设备接口 或需要调用其他地方的接口时,模拟请求登录获取接口的数据就很有必要。

    webclient类:只想从特定的URI请求文件,则使用WebClient;

    1.创建带Cookie的webclient:

        /// <summary>  
        /// 带 Cookie 的 WebClient  
        /// </summary>  
        public class CookieWebClient : WebClient  
        {  
            // Cookie 容器  
            public CookieContainer Cookies;  
          
            /// <summary>  
            /// 创建一个新的 CookieWebClient 实例。  
            /// </summary>  
            public CookieWebClient()  
            {  
                this.Cookies = new CookieContainer();  
            }  
          
            protected override WebRequest GetWebRequest(Uri address)  
            {  
                WebRequest request = base.GetWebRequest(address);  
                if (request is HttpWebRequest)  
                {  
                    HttpWebRequest httpRequest = request as HttpWebRequest;  
                    httpRequest.CookieContainer = Cookies;  
                }  
                return request;  
            }  
        }  

    2.http方法(Get、POST):

    public  CookieContainer cookies;
            internal string HttpGet(string Url)
            {
                string result = "";
                using (var client = new CookieWebClient())
                {
                    client.Encoding = Encoding.UTF8;
                    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded,charset=UTF-8";
                    client.Cookie = cookies;
                    byte[] data = client.DownloadData(Url);
                    result = Encoding.UTF8.GetString(data);
                } 
                return result;
            }
            internal string HttpPost(string Url, string postDataStr)
            {
                string result = "";
                using (var client = new CookieWebClient())
                {
                    client.Encoding = Encoding.UTF8;
                    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8";
                    byte[] post = Encoding.UTF8.GetBytes(postDataStr);
                    byte[] data = client.UploadData(Url,post);
                    result = Encoding.UTF8.GetString(data);
                    cookies = client.Cookie;
                }
                return result;
            }

    3. controller

     [HttpGet]
            public string Getinfo(string name, string pwd)
            {
                string hg = "";
                string url = "http://111.38.56.130:8084/LoginHandler.ashx?time=" + DateTime.Now;
                string loginp = "action=Login&flag=1&name={0}&pwd={1}&isRemember=false";
                string urlget = "http://111.38.56.130:8084/Company/IndexHandler.ashx?time={0}&action=deviceShowData&greenHouseSerialNum=00000F003G001";
                string param = string.Format(loginp, name, pwd);
                exta.http h = new exta.http();
                string r = h.HttpPost(url, param);
                if (r.Equals("0"))
                {
                    hg = h.HttpGet(string.Format(urlget, DateTime.Now));
                }
                return hg;
            }
  • 相关阅读:
    pygame 笔记-7 生命值/血条处理
    pygame 笔记-6 碰撞检测
    pygame 笔记-5 模块化&加入敌人
    pygame 笔记-4 代码封装&发射子弹
    tk.mybatis通用插件updateByPrimaryKeySelective无法自动更新ON UPDATE CURRENT_TIMESTAMP列的解决办法
    pygame 笔记-3 角色动画及背景的使用
    pygame 笔记-2 模仿超级玛丽的弹跳
    pygame 笔记-1 按键控制方块移动
    mysql技巧:如果记录存在则更新/如果不存在则插入的三种处理方法
    mac上mysql8.0以tar.gz方式手动安装
  • 原文地址:https://www.cnblogs.com/wangzhe688/p/7808634.html
Copyright © 2020-2023  润新知