Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > VB .NET > Вернуть HTML код странички в файл или массив


Автор: GOBLIN_TT 14.5.2008, 10:54
Народ всем привет.)
Помогите есть следующая проблема.

Необходимо вернуть HTML код странички, в массив или файл.
Например есть сайт, www.microsoft.com, с ХТМЛ кодом, <h1> Hello </h1>.

Заходим в нашу программу, с WebBrowser control, по этому адресу, и программа должна вернуть этот самый ХТМЛ код, "<h1> Hello </h1>".
Возможно ли такое слделать?

Help  smile 

Автор: Kosten 14.5.2008, 11:09
GOBLIN_TT, это возможно, только для этого не обязательно использовать WebBrowser control.
Я для таких целей работал с WebRequest и WebResponse.

Автор: GOBLIN_TT 14.5.2008, 11:11
Kosten - а есть простой пример?)

Автор: Kosten 14.5.2008, 12:23
Цитата(GOBLIN_TT @  14.5.2008,  11:11 Найти цитируемый пост)
Kosten - а есть простой пример?) 

нашел в MSDN

Код

' Create a 'WebRequest' object with the specified url 
Dim myWebRequest As WebRequest = WebRequest.Create("www.contoso.com")

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' Call method 'GetResponseStream' to obtain stream associated with the response object
Dim ReceiveStream As Stream = myWebResponse.GetResponseStream()

Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

' Pipe the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(ReceiveStream, encode)
Console.WriteLine(ControlChars.Cr + "Response stream received")
Dim read(256) As [Char]

' Read 256 charcters at a time    .
Dim count As Integer = readStream.Read(read, 0, 256)
Console.WriteLine("HTML..." + ControlChars.Lf + ControlChars.Cr)
While count > 0

    ' Dump the 256 characters on a string and display the string onto the console.
    Dim str As New [String](read, 0, count)
    Console.Write(str)
    count = readStream.Read(read, 0, 256)

End While
Console.WriteLine("")

' Release the resources of stream object.
 readStream.Close()

 ' Release the resources of response object.
myWebResponse.Close()


Автор: GOBLIN_TT 15.5.2008, 11:03
Слушай, неподскажеш куда именно копируется код ? smile     smile

Добавлено через 13 минут и 25 секунд
Всё вроде понял, надо было создать консольное приложение.  smile 

А под виндовс апликейшн будет работать данный код?

Автор: GOBLIN_TT 15.5.2008, 13:34
Всё я разобрался, большое тебе спасибо!  smile 

Вот код под винду. (если каму нужен)
Код

Imports System.Net
Imports System.IO
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create a 'WebRequest' object with the specified url 
        Dim myWebRequest As WebRequest = WebRequest.Create("http://goblin.tushino.com")

        ' Send the 'WebRequest' and wait for response.
        Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

        ' Call method 'GetResponseStream' to obtain stream associated with the response object
        Dim ReceiveStream As Stream = myWebResponse.GetResponseStream()

        Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

        ' Pipe the stream to a higher level stream reader with the required encoding format.
        Dim readStream As New StreamReader(ReceiveStream, encode)
        Console.WriteLine(ControlChars.Cr + "Response stream received")
        Dim read(256) As [Char]

        ' Read 256 charcters at a time    .
        Dim count As Integer = readStream.Read(read, 0, 256)
        Console.WriteLine("HTML..." + ControlChars.Lf + ControlChars.Cr)
        Dim i As Integer = 0
        Dim myStrings(100) As String
        While count > 0
            ' Dump the 256 characters on a string and display the string onto the console.
            Dim str As New [String](read, 0, count)
            Console.Write(str)
            count = readStream.Read(read, 0, 256)

            'RichTextBox1.Text = (str)
            i = i + 1
            myStrings(i) = str
            Me.RichTextBox1.Lines = myStrings

        End While
        Console.WriteLine("")

        ' Release the resources of stream object.
        readStream.Close()

        ' Release the resources of response object.
        myWebResponse.Close()



    End Sub
End Class


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)