【问题标题】:Call webpage from program从程序调用网页
【发布时间】:2013-08-18 21:14:18
【问题描述】:

我希望我的 c# 程序在打开网页时调用它。网页中的代码将增加一个计数器......因此我只需要程序来调用页面。不要认为我需要发布或获取或其他任何内容。

想法?

【问题讨论】:

标签: c#


【解决方案1】:

更多信息请查看msdn

// Create a request for the URL. 
WebRequest request = WebRequest.Create (
"http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream 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 and the response.
reader.Close ();
response.Close ();

如果你不能从上面提取你需要的代码..这里是

WebRequest request = WebRequest.Create ("http://www.mysite.com/counter.php?YourId");
WebResponse response = request.GetResponse();
response.Close ();

【讨论】:

  • 基本上网页是 www.mywebsite.com/counter.php?theID 我不在乎这个过程有多可靠。 counter.php 将获取 ID 并对其进行处理...我不需要传递凭据或任何东西。
【解决方案2】:

我知道它已经很晚了,但对于未来可能会陷入这种情况的人。

System.Net.WebClient client = new System.Net.WebClient();
client.DownloadDataAsync(new Uri("http://stackoverflow.com"));

【讨论】:

  • 我很好奇为什么这被否决了?如果有正当理由,我很乐意删除它。
猜你喜欢
  • 2021-04-04
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多