?
Solved

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

Posted on 2014-03-22
4
Medium Priority
?
557 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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