【问题标题】:visual studio c# interact with websitevisual studio c# 与网站交互
【发布时间】:2015-06-09 13:31:40
【问题描述】:

我想制作一个可以与现有网站交互的应用程序。该网站有一些与搜索相关的文本框字段,用户通常会在其中输入内容,然后单击按钮进入下一页。我想通过制作一个 Visual Studio C# 应用程序来自动化这个过程。我不确定从哪里开始,似乎大多数教程都旨在创建自己的网站,而不是与现有网站交互。

据我所知,在搜索中输入并单击按钮似乎不会创建唯一的 URL 字符串(不像 Google 地图那样),因为当我进入搜索时 URL 不会改变结果。我不确定我需要发送什么类型的 TCP 命令(我知道“GET”,但仅此而已)。

任何指针?

【问题讨论】:

  • 看看硒。
  • 正如丹尼尔建议的那样,这是一个快速入门scraping.pro/…
  • 看起来最简单的方法就是在 selenium dll 中引用,但我更想了解它是如何工作的。看起来源代码是开源的,所以也许我可以挖掘大量文件并弄清楚。

标签: c#


【解决方案1】:

在 URL 中输入文本将是一个 GET 请求。 TCP 只是 HTTP 的底层协议。

您正在寻找的是 HTTP POST 请求。

WebRequest开头。

// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();

postData 变量包含要“插入”到文本框中的元素。它应该给你一个好的开始。我一直在所有涉及网站交互的项目中使用这种技术。

cmets 中建议的

Selenium 可能是一种选择,前提是您不介意需要安装特定的浏览器,因为它会为此目的使用它。如果要部署应用程序,这不是一个好的解决方案。

【讨论】:

  • 谢谢!有了这个和一点点wireshark嗅探,我能够实现我的目标。
猜你喜欢
  • 1970-01-01
  • 2018-07-10
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多