2013-08-18 52 views
0

我希望我的c#程序在打開時調用網頁。網頁中的代碼會增加一個計數器...因此我只需要該程序調用該頁面即可。不要以爲我需要發佈或獲取或其他任何東西。從程序調用網頁

想法?

+0

您的要求不太清楚。你問如何從C#做一個GET?這個計數器是如何實施的? – vcsjones

+0

如果您要更改Web服務器的狀態,則需要使用POST。看看http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx –

+0

「網頁將增加一個計數器」聽起來不好 – wudzik

回答

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(); 
+0

基本上這個網頁是www.mywebsite.com/counter.php?the ID 我不在乎這個過程有多可靠。 counter.php將採取ID並做它的東西...我不需要傳遞憑據或任何東西。 – Sheldon

0

我知道它的晚,但對於未來的人誰可能會遇到這種情況。

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

我很好奇爲什麼這是投票下來?如果有正當理由,我會很樂意刪除它。 – Tony