문제

나는 이것이 기본적인 질문이어야한다는 것을 알고 있지만 나는 벽돌 벽을 때리고 있습니다. 파일을 열고 문자열 변수로 가져온 것처럼 결과 문자열을 다운로드하려고합니다.

나는 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
도움이 되었습니까?

해결책

C#의 짧은 대답은

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

다른 팁

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()

이 기능은 모든 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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top