Question : ASP code to VB 2010

Can any one advise how to convert this code to VB 2010?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
' The script can be called via

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<% call sendsms("447740123456",1,"123 ABC TEST test","","")

if AQresponse <> "" then
  response.write(AQresponse)
else
  response.write("ERROR")
end if

%>

' Actual script follows. This could be placed in a separate file, 
' such as the smslib.asp file described above

<%
response.buffer = true
' Copyright 2002 (aq) ltd.
' Script requires Microsoft XMLHTTP component

Dim method, secured, error_on_length, username, password, AQresponse
  ' User Editable Variables
  secured = 0                       ' Set to either 1 for SSL connection 
                                    ' or 0 for normal connection.
  error_on_length = 1               ' Whether to give and error on messages over 160 chracters. 
                                    ' 1 for true, 0 for false.
  username = "testusername"         ' Your aql username, can either be set here 
                                    ' or done on a per call basis from the function.
  password = "testpassword"         ' Your aql password, can either be set here
                                    ' or done on a per call basis from the function.

' Do not edit below here unless you know what you are doing!

Function sendsms(destination, flash, message, f_username, f_password, originator)
  if f_username <> null or f_username <> "" then
    username = f_username
  end if
  if f_password <> null or f_password <> "" then
    password = f_password
  end if
  if username = "" then
      call senderror(1)
    else if password = "" then
        call senderror(2)
    else if destination = "" then
        call senderror(3)
    else if len(message) > 160 and error_on_length = 1 then
        call senderror(6)
    else if flash > 1 or flash < 0 then
        call senderror(5)
    end if
    end if
    end if
    end if
  end if

Dim objXMLHTTP, xml
  message = replace(message," ","+")
  Set xml = Server.CreateObject("Microsoft.XMLHTTP")
  if secured = null or secured = 0 then
    xml.Open "POST", "http://gw1.aql.com/sms/sms_gw.php", False
    xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    xml.Send "username=" & username & "&password=" & password & "&destination=" & destination &
             "&message=" & message & "&originator=" & originator & "&flash=" & flash
  else if secured = 1 then
    xml.Open "POST", "https://gw1.aql.com/sms/sms_gw.php", False
    xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    xml.Send "username=" & username & "&password=" & password & "&destination=" & destination &
             "&message=" & message & "&originator=" & originator & "&flash=" & flash
  else
    call senderror(7)
    end if
   end if

AQresponse = xml.responseText
Set xml = nothing

End Function

Function senderror(id)
  set xml = nothing
  Select case(id)

  case (1)
    response.write
      ("No username was specified in either the function call or the config section")
    response.end
  case (2)
    response.write
      ("No password was specified in either the function call or the config section")
    response.end
  case (3)
    response.write ("No to number was set in the function call")
    response.end
  case (4)
    response.write("No, or incorrect method specified")
    response.end
  case (5)
    response.write("Invalid setting for Flash message flag, must be 1 or 0")
    response.End
  case (6)
    response.write("Message was over 160 chars and was not sent.")
    response.write("To disable this warning edit the flag in ""smslib.asp""")
    response.end
  case (7)
    response.write("Invalied setting for Secure flag, must be 1 or 0")
    response.end
end select
end function

%>

Answer : ASP code to VB 2010

It looks like that code is posting to a remote URL.  With VB.NET 2010, you could use the System.Net.HttpWebRequest class to achieve the same effect.

http://www.worldofasp.net/tut/WebRequest/Working_with_HttpWebRequest_and_HttpWebResponse_in_ASPNET_114.aspx

Sample Code on how to Post Data to remote Web Page using HttpWebRequest
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
Imports System.IO
Imports System.Net

Public Class RemotePost

    ''' <summary>
    ''' Post to a remote URL, and return the response text.
    ''' </summary>
    ''' <param name="url">A valid HTTP url</param>
    ''' <returns>The response from the remote web page</returns>
    Public Shared Function PostToUrl(ByVal url As String) As String
        Dim uri As New Uri(url)
        Dim data As String = "field-keywords=ASP.NET 2.0"
        If uri.Scheme = uri.UriSchemeHttp Then
            Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
            request.Method = WebRequestMethods.Http.Post
            request.ContentLength = data.Length
            request.ContentType = "application/x-www-form-urlencoded"
            Using writer As New StreamWriter(request.GetRequestStream)
                writer.Write(data)
            End Using
            Using response As HttpWebResponse = request.GetResponse()
                Using reader As New StreamReader(response.GetResponseStream())
                    Return reader.ReadToEnd()
                End Using
            End Using
        End If
        Return ""
    End Function

End Class
Random Solutions  
 
programming4us programming4us