2009-05-30 45 views
4

我知道這應該是一個基本問題,但我正在打磚牆。 我正在尋找去一個URL/URI下載結果字符串,就好像我打開了一個文件,然後把它取出到一個字符串變量。如何將網頁下載到.NET中的流中

我一直在塞滿IO.Stream和Net.httpxxx,但沒有設法讓元素以正確的方式排隊。

打開標準流中的頁面,我得到了「給定路徑的格式不被支持」,因爲它不在本地文件系統中......這一點我明白了,我沒有得到的是.. 。我該如何實現:

Public Function GetWebPageAsString(pURL As String) As String 
     Dim lStream As IO.StreamReader = New System.IO.StreamReader(pURL) 
     Return lStream.ReadToEnd 

End Function 
+0

謝謝大家,每一個解決方案所做的工作(最終)的一個我接受返回我尋找的是什麼,而不是我所要求的,這是我選擇它的原因。 (在VB中加2行)。 下一個VB傢伙的翻譯: Dim client As System.Net.WebClient = New System.Net.WebClient() Dim html As String = client.DownloadString(「http://www.google.com」) – 2009-05-31 02:32:45

+0

附加說明:我剛剛意識到我的一半問題是,我試圖獲取的URL具有「 - 」,在發出請求之前需要將其轉義。我可能不需要問我是否在昨晚半夜採摘了這個東西:) – 2009-05-31 02:40:46

回答

7

簡短的回答,在C#中,看起來像

using(System.Net.WebClient client = new System.Net.WebClient()) 
{ 
    string html = client.DownloadString("http://www.google.com"); 
} 
0

此函數將任何URI下載到文件。你可以很容易地適應它把它轉換成字符串VAR:

public static int DownloadFile(String remoteFilename, String localFilename, bool enforceXmlSafe) 
{ 
// Function will return the number of bytes processed 
// to the caller. Initialize to 0 here. 
int bytesProcessed = 0; 

// Assign values to these objects here so that they can 
// be referenced in the finally block 
Stream remoteStream = null; 
Stream localStream = null; 
WebResponse response = null;    

// Use a try/catch/finally block as both the WebRequest and Stream 
// classes throw exceptions upon error 
try 
{ 
    // Create a request for the specified remote file name 
    WebRequest request = WebRequest.Create(remoteFilename); 
    if (request != null) 
    { 
     // Send the request to the server and retrieve the 
     // WebResponse object 
     response = request.GetResponse(); 
     if (response != null) 
     { 
      // Once the WebResponse object has been retrieved, 
      // get the stream object associated with the response's data 
      remoteStream = response.GetResponseStream(); 

      // Create the local file 
      if (localFilename != null) 
       localStream = File.Create(localFilename); 
      else 
       localStream = new MemoryStream(); 

      // Allocate a 1k buffer 
      byte[] buffer = new byte[1024]; 
      int bytesRead; 

      // Simple do/while loop to read from stream until 
      // no bytes are returned 
      do 
      { 
       // Read data (up to 1k) from the stream 
       bytesRead = remoteStream.Read(buffer, 0, buffer.Length); 

       // Write the data to the local file 
       localStream.Write(buffer, 0, bytesRead); 

       // Increment total bytes processed 
       bytesProcessed += bytesRead; 
      } while (bytesRead > 0); 
     } 
    } 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e.Message); 
} 
finally 
{ 
    // Close the response and streams objects here 
    // to make sure they're closed even if an exception 
    // is thrown at some point 
    if (response != null) response.Close(); 
    if (remoteStream != null) remoteStream.Close(); 
    if (localStream != null) localStream.Close(); 
} 

// Return total bytes processed to caller. 
return bytesProcessed; 
} 
2

WebClient.OpenRead()可能是你在找什麼。從MSDN頁面

樣品上面鏈接:

Dim uriString as String 
uriString = "http://www.google.com" 

Dim myWebClient As New WebClient() 

Console.WriteLine("Accessing {0} ...", uriString) 

Dim myStream As Stream = myWebClient.OpenRead(uriString) 

Console.WriteLine(ControlChars.Cr + "Displaying Data :" + ControlChars.Cr) 
Dim sr As New StreamReader(myStream) 
Console.WriteLine(sr.ReadToEnd()) 

myStream.Close() 
+0

謝謝,但我得到了不支持給定路徑的格式 On Dim myStream As Stream = myWebClient.OpenRead(uriString)line for網頁http://www.alllotto.com/Arizona-Pick-3-May-2009-Lottery-Results.php 這是一個網頁的問題或? – 2009-05-30 14:49:43