none
Encoding

    Question

  • I honestly got a lot of questions for my project and I'm only getting trolled on the internet so I am here to see if I can get some guidance. On this site it shows me something that might work but I can't test it cause it says its not working so here is the copy past right off this site and ill explain what is not working

    Dim client As New WebClient()
            Dim pageData As [Byte]() = client.DownloadData("http://www.contoso.com")
            Dim pageHtml As String = Encoding.ASCII.GetString(pageData)
    
            ' Download the data to a buffer.
            Console.WriteLine(pageHtml)
    
            ' Download the data to a file.
            client.DownloadFile("http://www.contoso.com", "page.htm")
    

    The part where it says Encoding.ASCII.GetString(pageData) is not working because it has Encoding underlined so im guessing this command is not in VB.NET new version so anyone know how i can write this part to make it work so I can get back to pluggin away for the 3 straight day. Your time is valued and thank you

    Sunday, March 19, 2017 11:20 PM

Answers

  • You need several Imports e.g.

    Imports System.Net
    Imports System.Text
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim client As New WebClient()
            Dim pageData As [Byte]() = client.DownloadData("http://www.contoso.com")
            Dim pageHtml As String = Encoding.ASCII.GetString(pageData)
    
            ' Download the data to a buffer.
            Console.WriteLine(pageHtml)
    
            ' Download the data to a file.
            client.DownloadFile("http://www.contoso.com", "page.htm")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Frank L. Smith Sunday, March 19, 2017 11:45 PM
    • Marked as answer by Squish_E Monday, March 20, 2017 12:54 AM
    Sunday, March 19, 2017 11:35 PM
    Moderator
  • If you're just trying to read text (a string), and it doesn't require credentials, you might consider this:

    Imports System.IO
    Imports System.Net

    Private Function GetTextFromURL(ByVal url As String) As String Dim retVal As String = "" Try Dim request As WebRequest = WebRequest.Create(url) Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse) Using dataStream As Stream = response.GetResponseStream Using reader As New StreamReader(dataStream) Dim responseFromServer As String = reader.ReadToEnd() retVal = responseFromServer End Using End Using End Using Catch ex As Exception retVal = "" End Try Return retVal End Function



    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Squish_E Monday, March 20, 2017 12:54 AM
    Sunday, March 19, 2017 11:49 PM

All replies

  • You need several Imports e.g.

    Imports System.Net
    Imports System.Text
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim client As New WebClient()
            Dim pageData As [Byte]() = client.DownloadData("http://www.contoso.com")
            Dim pageHtml As String = Encoding.ASCII.GetString(pageData)
    
            ' Download the data to a buffer.
            Console.WriteLine(pageHtml)
    
            ' Download the data to a file.
            client.DownloadFile("http://www.contoso.com", "page.htm")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Frank L. Smith Sunday, March 19, 2017 11:45 PM
    • Marked as answer by Squish_E Monday, March 20, 2017 12:54 AM
    Sunday, March 19, 2017 11:35 PM
    Moderator
  • If you're just trying to read text (a string), and it doesn't require credentials, you might consider this:

    Imports System.IO
    Imports System.Net

    Private Function GetTextFromURL(ByVal url As String) As String Dim retVal As String = "" Try Dim request As WebRequest = WebRequest.Create(url) Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse) Using dataStream As Stream = response.GetResponseStream Using reader As New StreamReader(dataStream) Dim responseFromServer As String = reader.ReadToEnd() retVal = responseFromServer End Using End Using End Using Catch ex As Exception retVal = "" End Try Return retVal End Function



    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Squish_E Monday, March 20, 2017 12:54 AM
    Sunday, March 19, 2017 11:49 PM
  • Thank you! This is very much appreciated :) I thought those were already in VB.NET I guess that is what i get for assuming.
    • Edited by Squish_E Monday, March 20, 2017 12:55 AM
    Monday, March 20, 2017 12:55 AM