1
Reply

Sending post data via mobile application and parsing response

Matthew Bagley

Matthew Bagley

Mar 24 2009 11:37 AM
7.7k
Hey guys need some help here, I developed a while ago some software with Visual Studio 2008 and VB.Net were a user would login by sending their username and password to a php api system.  the software still works exactly as it should but I have been asked to create a mobile version of the software.  I figured it would work exactly as it is now but I cant get it to work at all.

The errors I get are :
Name 'HttpUtility' is not declared
The targeted version of the .NET Compact Framework does not support latebinding
'Settings' is not a member of 'My'

The code is below. (I have highlighted the errors on the correct areas of the code)

        Dim username As String
        Dim password As String
        Dim noerror As Integer = 0

        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim address As Uri
        Dim data As StringBuilder
        Dim byteData() As Byte
        Dim postStream As Stream = Nothing
        Dim HTTPresponse As String
        Dim HTTPresponseArray As Array
        Dim testing As ArrayList

        If (txtUsername.Text.Length > 3) And (txtPassword.Text.Length > 3) Then
            Button1.Enabled = False
            username = txtUsername.Text
            password = txtPassword.Text

            address = New Uri("REMOVED API ADDRESS")
            ' Create the web request 
            request = DirectCast(WebRequest.Create(address), HttpWebRequest)

            ' Set type to POST 
            request.Method = "POST"
            request.ContentType = "application/x-www-form-urlencoded"

            ' Create the data we want to send 

            data = New StringBuilder()
            data.Append("username=" + username)
            data.Append("&password=" + HttpUtility.UrlEncode(password))
            'data.Append("&password=" + password)

            ' Create a byte array of the data we want to send 
            byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())

            ' Set the content length in the request headers 
            request.ContentLength = byteData.Length
            ' Write data 
            Try
                postStream = request.GetRequestStream()
                postStream.Write(byteData, 0, byteData.Length)

            Catch ex As Exception
                MessageBox.Show("Unable to communicate to server.", "Communication Error")
                noerror = 1
                Button1.Enabled = True
            Finally
                If Not postStream Is Nothing Then postStream.Close()
            End Try

            If noerror = 0 Then
                Try
                    ' Get response 
                    response = DirectCast(request.GetResponse(), HttpWebResponse)

                    ' Get the response stream into a reader 
                    reader = New StreamReader(response.GetResponseStream())

                    ' Console application output 
                    HTTPresponse = reader.ReadToEnd()
                    ' If the first character of the response is 0 an error happened

                    If (HTTPresponse.Substring(0, 1) = "0") Then
                        HTTPresponseArray = HTTPresponse.Split("|")

                        MessageBox.Show(String.Concat("Unable to login, the system returned the following error", ControlChars.NewLine, ControlChars.NewLine, HTTPresponseArray(1)), "Error logging in")
                        Button1.Enabled = True
                    ElseIf (HTTPresponse.Substring(0, 1) = "1") Then
                        ' Logged in ok show the next form and setup some variables
                        'MessageBox.Show(HTTPresponse)
                        HTTPresponseArray = HTTPresponse.Split("|")
                        'MessageBox.Show(HTTPresponseArray(1))
                        'MessageBox.Show(testing(1))

                        My.Settings.globalkey = HTTPresponseArray(1)
                        My.Settings.lastTicket_timeStamp = "0"
                        My.Settings.loginTime = HTTPresponseArray(2).ToString
                        txtUsername.Text = ""
                        txtPassword.Text = ""
                        mainWindow.Show()

                    End If
                Catch ex As Exception
                    MessageBox.Show("Unable to login to system.  A communication error occured are you online?", "Error")
                    Button1.Enabled = True
                Finally
                    If Not response Is Nothing Then response.Close()
                End Try
            End If

        Else
            MessageBox.Show("Missing username or password, please try again", "Error")
        End If


How on earth can I get this working on the mobile framework as it is very annoying now :(

Answers (1)