程序HttpWebRequest [英] Procedural HttpWebRequest

查看:143
本文介绍了程序HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我首先要说的是,在HttpWebRequesting方面,我是一个非常新的和基本的。我有一个我想要自动化的调度程序。但为了让我这样做,我必须按以下顺序执行以下所有操作:

So I will start by saying I am very new and basic when it comes to HttpWebRequesting. I have a scheduler that I want to automate. But in order for me to do so, I have to do all of the following, in this order:

1)输入用户名和密码

1) enter Username and Password

1a)点击登录

2)选择带有2016文字的单选按钮

2) Select radio button with "2016" text

2a)点击继续

3)重新输入密码

3a)点击确定

4)输入时间表ID

4) enter schedule ID

4a)点击添加

毕竟这个网站通常在屏幕顶部显示一个绿色或红色标签,表示该项目是否正确安排。目前,我已经将所有这些与WebBrowser控件一起使用,并且我正在清除每个项目的所有元素和属性,以检查它是否是输入数据的正确字段。但是,我最近被告知使用HttpRequests要快得多。

After all this the website usually displays a green or red label at the top of the screen representing whether the item was scheduled right or not. At the moment, I have all this working with the WebBrowser Control, and I am scraping off all the Elements and Attributes for each item to check to see if it is the proper field to enter data into. However, I have been recently told that using HttpRequests would be much much much faster.

在研究了一些之后,我遇到此问题进行身份验证。我是在正确的轨道吗?

After researching around some, I came across this for authentication. Am I on the right track??

任何建议或提示??

推荐答案

您应该打开浏览器网络监视器,看看请求
使用的方法是什么是传递的参数的名称,并在 HttpWebRequest 对象中模拟它,我假设你已经知道怎么做 GET 方法,
这里是你如何做一个基本的 POST 请求 HttpWebRequest

You should open your browser network monitor see whats the method the request have used what's the name of the passed parametres and simulate that in your HttpWebRequest object, i assume you already know how to do GET method, here is how you do a basic POST request with HttpWebRequest

var cookies = new CookieContainer();
var req = (HttpWebRequest)WebRequest.Create("https://example.com/login");
req.CookieContainer = cookies; 
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";

var buffer = Encoding.UTF8.GetBytes("user=foo&pass=bar"); //passed params
req.ContentLength = buffer.Length;
using (var reqStream = req.GetRequestStream()) {
      reqStream.Write(buffer, 0, buffer.Length);
}

using (var res = req.GetResponse())
using (var resStream = res.GetResponseStream())
using (var reader = new StreamReader(resStream)) {
       var respStr = reader.ReadToEnd();
}

你应该使用相同的 CookieContainer 所有请求的对象,
respStr 现在是服务器文本响应,您也可以在请求中发送其他标头(使用 req.Headers.AddHeader()如果需要),或者设置用户代理或更改内容类型等,这取决于您在网络监视器中找到的内容,或者您​​可以使用桌面应用程序喜欢 fiddler

and you should use the same CookieContainer object for all requests, respStr is now the server text response, you may also send additional headers within the request (use req.Headers.AddHeader() if needed), or set the user-agent or change the content-type, etc that depends on what you find in the network monitor or you may use a desktop application like fiddler.

这篇关于程序HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆