i have issues here to webrequest login

Ricardo Sousa 76 Reputation points
2025-12-05T20:46:32.73+00:00

Hi there i want to use this for web request to login on the browser and load the form 2 as it explain here add credentials and login accept load form 2 if any error responds back the error

and loads the Frm_Error as messagebox

            Dim username As String = TxtBox_Username.Text  ' Username 
            Dim password As String = TxtBox_Password.Text  ' Password 
            Dim url As String = "http://hostname" ' Any Hostname or IP Address 
            Dim Ports As String = 8000 ' Any Port as you want 
           
            Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest) ' Create Url Hostname to Connect to the Server 
            Dim Port As HttpWebRequest = CType(WebRequest.Create(Ports), HttpWebRequest)  ' Create Ports Numbers 80 or other Port to Remote Server 
            request.Credentials = New NetworkCredential(username, password)  ' Using Username and Password to use Credentials  
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            Dim Frm_Error As String
            Frm_Error = (CType(response.StatusCode, Integer))   'Console.WriteLine(CType(response.StatusCode, Integer))
            Using reader As New StreamReader(response.GetResponseStream())
                Dim responseText As String = reader.ReadToEnd()
                Form2.Show()
                Me.Hide()
                'Console.WriteLine(responseText)
            End Using
            response.Close()
        End If
Developer technologies | Visual Basic for Applications
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2025-12-05T20:46:45.35+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To implement a web request for logging in and handling errors in Visual Basic, you can follow the structure you provided, but there are a few adjustments needed to ensure proper functionality. Here’s a refined version of your code:

    Dim username As String = TxtBox_Username.Text  ' Username 
    Dim password As String = TxtBox_Password.Text  ' Password 
    Dim url As String = "http://hostname:8000" ' Complete URL with port
    
    Try
        Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)  ' Create Url Hostname to Connect to the Server 
        request.Credentials = New NetworkCredential(username, password)  ' Using Username and Password to use Credentials  
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
    
        Using reader As New StreamReader(response.GetResponseStream())
            Dim responseText As String = reader.ReadToEnd()
            Form2.Show()  ' Load Form2 on successful login
            Me.Hide()
        End Using
        response.Close()
    Catch ex As WebException
        Dim errorResponse As HttpWebResponse = CType(ex.Response, HttpWebResponse)
        Dim Frm_Error As String = "Error: " & CType(errorResponse.StatusCode, Integer).ToString()  ' Capture error status code
        MessageBox.Show(Frm_Error)  ' Show error message box
    End Try
    

    Key Changes:

    1. URL Construction: Ensure the URL includes the port directly in the string (e.g., "http://hostname:8000").
    2. Error Handling: Use a Try...Catch block to handle exceptions that may occur during the request. This allows you to catch WebException and display the error message appropriately.
    3. Response Handling: Ensure that you close the response in the Finally block or after you are done using it to free up resources.

    This structure will help you manage the login process and handle any errors effectively, displaying them in a message box as required.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.