Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

How to convert vb code to vb.net code for webb

I want to learn more about the Webrequest-class and therefore I tried to convert the  code Example on MSDN which so it works in Visual studio 2012 Express for Web.

MSDN Webrequest class

The original code looks like this

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
    Public Class WebRequestGetExample

        Public Shared Sub Main()
            ' Create a request for the URL. 		 
            Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
            ' If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials
            ' Get the response. 
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            ' Display the status.
            Console.WriteLine(response.StatusDescription)
            ' Get the stream containing content returned by the server. 
            Dim dataStream As Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access. 
            Dim reader As New StreamReader(dataStream)
            ' Read the content. 
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Cleanup the streams and the response.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub 'Main
    End Class 'WebRequestGetExample
End Namespace

Open in new window


My biggest problem is to understand what to to with these parts of the original code:
Namespace Examples.System.Net
    Public Class WebRequestGetExample
[...]

   End Class 'WebRequestGetExample
End Namespace

Open in new window


I cannot place them within the script-tag

And  lines like these

Console.WriteLine(response.StatusDescription)

Open in new window


I cannot write Response.write(response.StatusDescription)


Here is what I have tried to do:


<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>


<!DOCTYPE html>

<script runat="server">
   
    
    Sub Page_load() Handles Me.Load
        ' Create a request for the URL. 		 
        Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
        ' If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials
        ' Get the response. 
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        ' Display the status.
        Console.WriteLine(response.StatusDescription)
        ' Get the stream containing content returned by the server. 
        Dim dataStream As Stream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access. 
        Dim reader As New StreamReader(dataStream)
        ' Read the content. 
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        Console.WriteLine(responseFromServer)
        ' Cleanup the streams and the response.
        reader.Close()
        dataStream.Close()
        response.Close()
    End Sub

  
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window


I would be very grateful for a clear explanation so I can convert other examples easily as well.
0
marcgu
Asked:
marcgu
  • 2
  • 2
1 Solution
 
CodeCruiserCommented:
That is a VB.NET code that you have linked to on MSDN. The only difference is that the example is for a console application so you can just leave out the console.writeline statements.
0
 
marcguAuthor Commented:
Hi!

I donĀ“t want to leave out out the console.writeline statements since I want to understand the example completely. If I leave out the console.writeline statements this part will be missing:

' Display the content.
        Console.WriteLine(responseFromServer)

Open in new window


If I change it to response.write(responseFromServer)  it will be blue underlined with the message "'Write is not a member of  System.net.Http.Webresponse"

What I have done is to convert this part

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

Open in new window


to

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>

Open in new window


I also added this

<%@ Page Language="VB" %>
<!DOCTYPE html>

Open in new window


I left out these parts because I was not sure where to place them. It seems like I have to place the Namespace -part outside the script-tag but if I try to place the class example within the script-tag I get the following error: "Event  load cannot be found"

Namespace Examples.System.Net
    Public Class WebRequestGetExample

End Class 'WebRequestGetExample
End Namespace

Open in new window


I also changed the

Public Shared Sub Main()
end sub

Open in new window


to

Sub Page_load() Handles Me.Load
 End Sub

Open in new window


and placed them within script-tag.

I also added some html-code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window


My questions are basically:

1. How do I convert the console.writeline so that it also works in webb-application (this is the most important thing for me to understand. Why cant I use Response.write() .

2. Where to place the Namespace part when converting this into a aspx.form page with no code-behind page

3. Where to place the class-parts of the code

4. Is it not possible to convert this example from a console -application to a webbaplication?

Hope it is clearer now what I mean.
0
 
CodeCruiserCommented:
1) Get rid of the <%@ Import Namespace="System.Net" %> and Response.Write should work.
2) You don't have to use a namespace. If you still want to use it, switch to code behind file instead of using script tag.
3) In a code behind file.
4) It is. You just need to fix the above issues.

http://blogs.msdn.com/b/mikhailarkhipov/archive/2006/04/13/576117.aspx
0
 
marcguAuthor Commented:
Hi!

Thanks for answer 2 and 3. I, however think was not possible to get rid of the namespace as suggested in 1. Instead I made these changes
 
Dim responses As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)  instead of dim response  As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

Dim dataStream As Stream = responses.GetResponseStream() instead of dim datastream As Stream =response.GetResponseStream()

responses.Close() instead of response.close

To me it seems like the problem was that I try to use Response.write and before that declare a variable with the name response


I will leave it open for a couple of days to give further comments else I will assign you the points for the important clues on how to imporve the code

New improved code

    Sub Page(Sender as Object, e as Eventargs) handles Me.load 
        ' Create a request for the URL. 		 
        Dim request As WebRequest = WebRequest.Create("http://www.google.com")
        ' If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials
        ' Get the response. s
        Dim responses As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        ' Display the status.
        Response.Write(response.StatusDescription)
        ' Get the stream containing content returned by the server. 
        Dim dataStream As Stream = responses.GetResponseStream()
        ' Open the stream using a StreamReader for easy access. 
        Dim reader As New StreamReader(dataStream)
        ' Read the content. 
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        Response.Write(responseFromServer)
        ' Cleanup the streams and the response.
        reader.Close()
        dataStream.Close()
        responses.Close()
    End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now