• C#模拟httpwebrequest请求_向服务器模拟cookie发送


    使用C#代码模拟web请求,是一种常用的方法,以前没专门整理过,这里暂时贴上自己整理的完整代码,以后再做梳理:

    public class MyRequest
        {
            #region 辅助方法
            public static string HttpGet(string url)
            {
                var request = (HttpWebRequest)WebRequest.Create(url);
    
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                return responseString;
            }
            /// <summary>
            /// httpPost请求--参数为object
            /// </summary>
            /// <param name="url">地址</param>
            /// <param name="postObject">Post参数传输为对象</param>
            /// <returns></returns>
            public static string HttpPost(string url, object postObject, string at = "", string rt = "")
            {
                string result = string.Empty;
    
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(url);
                    var postData = JsonConvert.SerializeObject(postObject);
    
                    var data = Encoding.UTF8.GetBytes(postData);  //uft-8支持中文
                    request.Method = "POST";
                    //request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentType = "application/json;charset=UTF-8";
                    //request.ContentLength = data.Length;
    
                    //这里使用了coolie容器,用来模拟向服务器发送cookie信息
                    CookieContainer zl_Cookie = new CookieContainer();
                    zl_Cookie.Add(new Cookie("at", at, "/", ".zhaopin.com"));
                    zl_Cookie.Add(new Cookie("rt", rt, "/", ".zhaopin.com"));
                    request.CookieContainer = zl_Cookie;
                    
                    using (var stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    result = new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
                //Console.WriteLine("发送消息结果:" + result);
                return result;
            }
            /// <summary>
            /// httpPost请求--参数为string
            /// </summary>
            /// <param name="url">地址</param>
            /// <param name="postString">post参数参数为字符串</param>
            /// <returns></returns>
            public static string HttpPost(string url, string postString)
            {
                string result = string.Empty;
    
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(url);
    
                    var data = Encoding.UTF8.GetBytes(postString);  //uft-8支持中文
                    request.Method = "POST";
                    //request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentType = "application/json;charset=UTF-8";
                    request.ContentLength = data.Length;
    
                    using (var stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    result = new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                    Console.WriteLine("同步签到大屏发送消息error:" + result);
                }
    
                return result;
            }
            #endregion
    
        }
  • 相关阅读:
    模型绑定
    Asp.Net MVC基础
    Entity Framework 6.x介绍
    IE内核发送ajax请求时不会将url中的参数编码
    .NET使用OpenSSL生成的pem密钥文件[1024位]
    简单测试nginx1.90做TCP协议负载均衡的功能
    《图解TCP/IP》读书笔记
    使用wireshark抓包分析浏览器无法建立WebSocket连接的问题(server为Alchemy WebSockets组件)
    wireshark常用的过滤器设置
    通过监控线程状态来保证socket服务器的稳定运行
  • 原文地址:https://www.cnblogs.com/lxhbky/p/8372442.html
Copyright © 2020-2023  润新知