如何根据 C# 中的名称值使用 POST 提交 Web 请求?

发布于 2024-12-15 04:45:05 字数 1675 浏览 3 评论 0原文

我一直在阅读有关使用网络请求以及发布和获取方法来提交在线表单的信息,但我非常迷失。该网络表单驻留在充满其他超链接的页面上,但查看源代码,它在一个位置显示 method=get ,在另一个位置显示 method=post 。 method=get 位于有关搜索栏的信息附近的代码中,因此我相信这适用于该函数。我想在提交之前填写的 2 个文本字段被列为“name=weblink”和“name=imageurl”,因此我看到的许多使用 id 值的示例似乎并不适用。 “保存”按钮被标识为

input type="submit" value='Save Changes'

当前,这就是我的代码的样子:

try
            {
//store our text box information as variables to be uploaded
                string picture = txtbxPicture.Text,
                 weblink = txtbxWeblink.Text,
                 uriString = txtbxWebLink.Text;

                // Create a new WebClient instance.
                WebClient myWebClient = new WebClient();

                // Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
                NameValueCollection collection = 
                    new System.Collections.Specialized.NameValueCollection();

                collection.Add("weblink", weblink);
                collection.Add("imageurl", picture);                   

                byte[] result = myWebClient.UploadValues(uriString, collection);

            }
            catch (FormatException ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }

如果我正确理解所有内容,我将使用 NameValueCollection 根据其“名称”查找字段?此代码主要使用 MSDN 信息进行编译:http://msdn.microsoft.com /en-us/library/9w7b4fz7.aspx

我见过很多其他使用编码的例子,并明确指出它是一个“POST”方法,但MSDN说它是暗示?我基本上迷失在这里了。有人可以提供一些指导吗,当前的线程只是让我更加困惑。

更新 - 所以识别帖子似乎没有必要,但是是可能的。单击提交按钮会怎么样?我需要编码但不需要编码?我不明白上面的代码需要做什么才能完成该过程。

I have been reading up on using webrequests and post and get methods to submit an online form but I am very lost. The webform resides on a page full of other hyperlinks, but viewing the source code it says method=get in one spot and method=post in another. The method=get is located in the code near information regarding a search bar though so I believe that applies to that function. The 2 text fields I want to fill before submitting are listed as "name=weblink" and "name=imageurl", so lots of the examples I've seen using id values doesn't seem to apply. The "Save" button is identified as

input type="submit" value='Save Changes'

Currently this is what my code looks like:

try
            {
//store our text box information as variables to be uploaded
                string picture = txtbxPicture.Text,
                 weblink = txtbxWeblink.Text,
                 uriString = txtbxWebLink.Text;

                // Create a new WebClient instance.
                WebClient myWebClient = new WebClient();

                // Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
                NameValueCollection collection = 
                    new System.Collections.Specialized.NameValueCollection();

                collection.Add("weblink", weblink);
                collection.Add("imageurl", picture);                   

                byte[] result = myWebClient.UploadValues(uriString, collection);

            }
            catch (FormatException ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }

If I understand everything correctly I'm using the NameValueCollection to look for fields based on their "name"? This code was compiled using primarily MSDN's info here: http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx

I have seen a lot of other examples that use encoding, and explicitly identify that it is a "POST" method, but MSDN says it's implied? I'm basically lost here. Can someone offer a little guidance, the current threads out there are just making me more confused.

UPDATE - So identifying post doesn't seem to be necessary, but is possible. What about clicking the submit button is that something that I need to be coding in but am not? I don't understand what my code above needs to do in order to complete the process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2024-12-22 04:45:05

默认情况下,WebClient.UploadValues() 将使用 HTTP 上的 POST,您不必指定(如果需要,您可以这样做,该方法的重载确实允许您这样做)。

来自 MSDN

该方法使用STOR命令上传FTP资源。 对于一个
HTTP资源,使用POST方法。

WebClient.UploadValues() will use POST over HTTP by default, you don't have to specify that (you may though if you need to, there's an overload of the method that does allow you to).

From MSDN:

This method uses the STOR command to upload an FTP resource. For an
HTTP resource, the POST method is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文