Web 客户端上传值
我想制作一个桌面应用程序,在文本框中输入值并执行按钮操作,例如设计一个应用程序,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UploadValues
正在尝试执行POST
(至少默认情况下;允许使用其他一些动词,但它们本质上仍然将其视为正文有效负载)。 听起来就像您只想要一个GET
查询,例如http://www.google.com/search?q=afnan - 所以只需对"afnan"
进行 url 编码即可。但请注意,您应该始终遵守目标网站的条款和条件 - 特别是第 5 节:,如果您这样做,预计会被列入黑名单。
UploadValues
is trying to do aPOST
(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 aGET
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:If you do this, expect to get black-listed.