模拟Post/get提交数据 并抓取返回数据

本文介绍了如何使用 HTTP Web Request 和 Response 模拟用户登录过程并提交数据,包括构建 POST 请求数据、设置请求头、发送请求和接收响应的基本步骤。

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

最近需要做的项目中,使用httpwebrequest httpwebrespone来模拟提交数据

还有模拟用户登录后提交数据。

下面做个简单一点的。模拟post提交数据,get在前面的抓取sina的天气预报也使用到了。

下面是代码




//提交的post数据

            string postData = string.Format("account={0}&reaccount={0}&cardcode={1}&cardpassword={2}",txtLoginId.Text,txtCardNo.Text,txtPass.Text);
            postData += string.Format("&__EVENTTARGET={0}&__EVENTARGUMENT=&__VIEWSTATE={1}", "nextStep", "YToxOntzOjExOiJjdXJyZW50VXNlciI7YjowO30=");
            HttpWebResponse response;
            HttpWebRequest request;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);
            request = (HttpWebRequest)WebRequest.Create("url");
            data = encoding.GetBytes(postData);
            request.Method = "POST";
            request.ProtocolVersion = HttpVersion.Version10;
            request.UserAgent = "Mozilla/4.0";


            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();
            string html = string.Empty;
            try
            {
                //获取服务器返回的资源
                response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                html = reader.ReadToEnd();
                reader.Close();
                response.Close();
                textBox1.Text = html;
            }
            catch (Exception ex)
            {
                label4.Text = ex.Message;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值