Web 客户端上传值

发布于 2024-10-08 20:02:40 字数 413 浏览 0 评论 0原文

我想制作一个桌面应用程序,在文本框中输入值并执行按钮操作,例如设计一个应用程序,在 google.com 的谷歌搜索框中输入值并执行操作,就像任何人按下搜索按钮时一样,我编写了一段代码,但它抛出了异常 远程服务器返回错误:(405) 方法不允许。

WebClient wc = new WebClient();
string uri = "http://google.com";

NameValueCollection nvc = new NameValueCollection();

nvc.Add("search", "afnan");            

byte[] response = wc.UploadValues(uri, nvc);
textBox1.Text=Encoding.ASCII.GetString(response);

I want to make a desktop application that enters a value in textbox and performs button actions for example design an application that enters value in google search box at google.com and performs action as when any one presses search button i wrote a code but it threw exception The remote server returned an error: (405) Method Not Allowed.

WebClient wc = new WebClient();
string uri = "http://google.com";

NameValueCollection nvc = new NameValueCollection();

nvc.Add("search", "afnan");            

byte[] response = wc.UploadValues(uri, nvc);
textBox1.Text=Encoding.ASCII.GetString(response);

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

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

发布评论

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

评论(1

紫瑟鸿黎 2024-10-15 20:02:40

UploadValues 正在尝试执行 POST (至少默认情况下;允许使用其他一些动词,但它们本质上仍然将其视为正文有效负载)。 听起来就像您只想要一个GET查询,例如http://www.google.com/search?q=afnan - 所以只需对 "afnan" 进行 url 编码即可。但请注意,您应该始终遵守目标网站的条款和条件 - 特别是第 5 节:

您明确同意不通过任何自动化方式(包括使用脚本或网络爬虫)访问(或尝试访问)任何服务

,如果您这样做,预计会被列入黑名单。

UploadValues is trying to do a POST (by default, at least; some other verbs are allowed, but they essentially still treat it as a body payload). It sounds like you just want a GET query like http://www.google.com/search?q=afnan - so just url-encode "afnan". Note, however, that you should always observe the target site's Terms and Conditions - in particular section 5:

You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers)

If you do this, expect to get black-listed.

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