C# 从 Web 客户端单击 HTML 按钮 [英] C# Click HTML Button from Webclient

查看:45
本文介绍了C# 从 Web 客户端单击 HTML 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Windows 窗体应用程序.我希望 webclient 发布 NameValueCollection 中的值,然后点击提交按钮.但现在它只将值添加到 html 但从不提交它们.我如何模拟这个按钮点击?

I'm working on a Windows Forms Application. I want the webclient to post the values from the NameValueCollection and then hit the submit button. But now it only adds the values to the html but never submits them. How can I simulate this buttonclick?

private void button2_Click(object sender, EventArgs e)
        {
            var requestData = new NameValueCollection
            {
                {"prodids", "somevalue" },
                {"customerid", "somevalue" },
                {"submit_button", "submit" }
            };
            byte[] request = client.UploadValues("myurl", "POST", requestData);
            string result = Encoding.UTF8.GetString(request);

推荐答案

你需要获取包含你按钮的

元素,然后获取它的 action> 属性.这是您的客户应该向其发出请求的 URL.您可能还需要获取 action 属性来确定是发出 GET 请求还是 POST 请求.

You need to get the <form> element that contains you button, and then get its action attribute. That's the URL that your client should make a request to. You might need to get also the action attribute to find out whether to make GET or POST request.

问题是,按钮本身并不重要:在网络浏览器中,如果它有提交"动作,它只会触发包含表单来序列化内容并将它们发送到 action url 使用`方法.

The thing is, the button itself is not important: in the web browser, if it has action "submit" it just triggers the containing form to serialize contents and send them to the action url using `method.

当您使用客户端与网页进行交互时,您不能将其视为网络浏览器,而更像是下载页面并使用文本编辑器打开它.没有什么是可点击的,没有 JS,甚至没有任何东西被渲染——它只是从服务器发送的原始内容.

When you use a client to interact with a webpage, you cannot be thinking of it like of a web browser, more like downloading a page and opening it with a text editor. Nothing's clickable, there's no JS, there's nothing even rendered - it's just the raw content sent from the server.

所以,这完全是用 JavaScript 来完成的,这是完全错误的.无论如何,您的方法是 POST,您的操作是 /View/DashboardProxy.php?location=Dashboard/RequestServlet&postdata=1,因此您的调用将是:byte[] request = client.UploadValues("/View/DashboardProxy.php?location=Dashboard/RequestServlet&postdata=1", "POST", requestData);

So, this is done purely with JavaScript, which is all kinds of wrong. Anyhow, your method is POST and your action is /View/DashboardProxy.php?location=Dashboard/RequestServlet&postdata=1, so your call will be: byte[] request = client.UploadValues("/View/DashboardProxy.php?location=Dashboard/RequestServlet&postdata=1", "POST", requestData);

请注意,响应不会是一个完整的页面,但可能没有或一些文本要放入 post_result_textarea.

Note, that the response will not be a full page, but possibly nothing or some text to put in post_result_textarea.

哦,还要注意在该 POST 请求中传递了 3 个以上的值 - 值来自:server_id、prodids、shopid、customerid 和 specialbids.可能服务器要求填写所有这些字段.

Oh, and also note that there are more than 3 values passed in that POST request - values from: server_id, prodids, shopid, customerid and specialbids. Possibly the server requires all of those fields to be filled.

这篇关于C# 从 Web 客户端单击 HTML 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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