Solved

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

Posted on 2014-03-22
4
548 Views
Last Modified: 2014-04-06
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
Comment
Question by:marcgu
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39948833
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
 

Author Comment

by:marcgu
ID: 39949069
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39949924
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
 

Author Comment

by:marcgu
ID: 39954512
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now