C# 模拟发送Http请求获取请求页面

本文介绍了一种业务分包发送的技术实现方式,包括使用Fiddler4进行抓包及VS2010环境下通过WebRequest进行数据提交的具体方法。文中详细展示了如何设置请求头、提交数据并获取响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目用到的一个业务分包发送技术点,现在记录下来,方便以后回顾

1、工具Fiddler4(抓包工具)

2、VS2010

提交核心方法:

  /// <summary>

        /// 使用webrequest进行数据提交

        /// </summary>

        /// <param name="url">提交的地址</param>

        /// <param name="url_referer">提交的源地址</param>

        /// <param name="data">提交的数据</param>

        /// <param name="Mehtod">提交模式</param>

        /// <returns></returns>

 public static string postHttpC(string url, string url_referer, string data, string Mehtod = "POST", bool xml = false, string head = "")

        {//encodeURI

            if (State) return "提交停止";

            System.GC.Collect();

            HttpWebRequest httpwebRequest = null;

            HttpWebResponse httpwebResponse = null;

            StreamReader streamReader = null;

            string responsecontent = "";

            try

            {

                httpwebRequest = (HttpWebRequest)WebRequest.Create(Ip + url);

                if (url_referer != "" && url_referer != null)

                {

                    httpwebRequest.Referer = Ip + url_referer;

                }

                httpwebRequest.Method = Mehtod;

                if (!head.Equals("")) { httpwebRequest.ContentType = head; }

                else

                {

                    httpwebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; //text / plain;

                }

                httpwebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*";

                httpwebRequest.UserAgent = "Mozilla/4.0Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/8.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Tablet PC 2.0)";

                //image/gif, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*

 

                httpwebRequest.Headers["Cache-Control"] = "no-cache";

                httpwebRequest.Headers["Accept-Language"] = "zh-CN";

                httpwebRequest.Headers["Accept-Encoding"] = "gzip, deflate";

                if (xml)

                {

                    httpwebRequest.Headers["X-Requested-With"] = "XMLHttpRequest";

                    //httpwebRequest.Headers["aaxmlrequest"] = "true";

                }

                httpwebRequest.KeepAlive = true;

                httpwebRequest.Headers["Cookie"] = Cookie;//赋值cookeis

                Encoding encoding = Encoding.GetEncoding("utf-8");

                if (data != "" && data != null)

                {

                    byte[] postData = encoding.GetBytes(HttpUtility.UrlEncode(data, Encoding.UTF8).setenCode());

                    httpwebRequest.ContentLength = postData.Length;

                    Stream requeStream = httpwebRequest.GetRequestStream();

                    requeStream.Write(postData, 0, postData.Length);

                }

 

                httpwebResponse = (HttpWebResponse)httpwebRequest.GetResponse();

                if (httpwebResponse.CharacterSet.ToUpper().Equals("UTF-8"))

                {

                    encoding = Encoding.Unicode;

                }

                streamReader = new StreamReader(httpwebResponse.GetResponseStream());

                responsecontent = streamReader.ReadToEnd();

            }

            catch { return "提交异常"; }

            finally

            {

                if (httpwebResponse != null)

                {

                    httpwebResponse.Close();

                }

                if (streamReader != null)

                {

                    streamReader.Close();

                }

                if (httpwebRequest != null)

                {

                    httpwebRequest.Abort();

                }

                httpwebRequest = null;

                httpwebResponse = null;

                streamReader = null;

            }

            return responsecontent;

        }

 

 

抓包头部信息获取



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_陈陆亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值