VB6 working & Vb.net not working

shamuntoha

New member
Joined
Aug 9, 2008
Messages
2
Programming Experience
Beginner
Can anyone tell me why this working in VB 6, but i cant use it in Vb.net where msgbox is empty, and in apache server i dont receive the hit for this request.

VB.NET:
Fomr 1
-------
Dim vlogin As String
vlogin = 
Module1.PostInfo("host.domain.com", "/folder/login.php?user=root&pass=root&submit=submit", "")

MsgBox(vlogin)


'Mobule 1
-------------
Option Explicit On

Module Module1

    Private Declare Function _
       InternetOpen Lib "wininet.dll" _
     Alias "InternetOpenA" _
      (ByVal lpszCallerName As String, _
       ByVal dwAccessType As Long, _
       ByVal lpszProxyName As String, _
       ByVal lpszProxyBypass As String, _
       ByVal dwFlags As Long) As Long

    Private Declare Function InternetConnect Lib "wininet.dll" _
      Alias "InternetConnectA" _
      (ByVal hInternetSession As Long, _
       ByVal lpszServerName As String, _
       ByVal nProxyPort As Integer, _
       ByVal lpszUsername As String, _
       ByVal lpszPassword As String, _
       ByVal dwService As Long, _
       ByVal dwFlags As Long, _
       ByVal dwContext As Long) As Long

    Private Declare Function InternetReadFile Lib "wininet.dll" _
      (ByVal hFile As Long, _
       ByVal sBuffer As String, _
       ByVal lNumBytesToRead As Long, _
       ByVal lNumberOfBytesRead As Long) As Integer

    Private Declare Function HttpOpenRequest Lib "wininet.dll" _
       Alias "HttpOpenRequestA" _
       (ByVal hInternetSession As Long, _
        ByVal lpszVerb As String, _
        ByVal lpszObjectName As String, _
        ByVal lpszVersion As String, _
        ByVal lpszReferer As String, _
        ByVal lpszAcceptTypes As Long, _
        ByVal dwFlags As Long, _
        ByVal dwContext As Long) As Long

    Private Declare Function HttpSendRequest Lib "wininet.dll" _
     Alias "HttpSendRequestA" _
     (ByVal hHttpRequest As Long, _
      ByVal sHeaders As String, _
      ByVal lHeadersLength As Long, _
      ByVal sOptional As String, _
      ByVal lOptionalLength As Long) As Boolean

    Private Declare Function InternetCloseHandle Lib "wininet.dll" _
     (ByVal hInternetHandle As Long) As Boolean

    Private Declare Function HttpAddRequestHeaders Lib "wininet.dll" _
      Alias "HttpAddRequestHeadersA" _
      (ByVal hHttpRequest As Long, _
       ByVal sHeaders As String, _
       ByVal lHeadersLength As Long, _
       ByVal lModifiers As Long) As Integer

    'This is used for HTTP, HTTPS, FTP
    'Primitive to use
    'srv = ip or host
    'script = /folder/index.php?username=username&password=password&submit=submit
    'postdat =""
    'output = msgbox postinfo
    Public Function PostInfo(ByVal srv$, ByVal script$, ByVal postdat$) As String

        Dim hInternetOpen As Long
        Dim hInternetConnect As Long
        Dim hHttpOpenRequest As Long
        Dim bRet As Boolean

        hInternetOpen = 0
        hInternetConnect = 0
        hHttpOpenRequest = 0

        'Use registry access settings.
        Const INTERNET_OPEN_TYPE_PRECONFIG = 0
        hInternetOpen = InternetOpen("http generic", _
                        INTERNET_OPEN_TYPE_PRECONFIG, _
                        vbNullString, _
                        vbNullString, _
                        0)

        If hInternetOpen <> 0 Then
            'Type of service to access.
            Const INTERNET_SERVICE_HTTP = 3
            Const INTERNET_DEFAULT_HTTP_PORT = 80
            'Change the server to your server name
            hInternetConnect = InternetConnect(hInternetOpen, _
                               srv$, _
                               INTERNET_DEFAULT_HTTP_PORT, _
                               vbNullString, _
                               "HTTP/1.0", _
                               INTERNET_SERVICE_HTTP, _
                               0, _
                               0)

            If hInternetConnect <> 0 Then
                'Brings the data across the wire even if it locally cached.
                Const INTERNET_FLAG_RELOAD = &H80000000
                hHttpOpenRequest = HttpOpenRequest(hInternetConnect, _
                                    "GET", _
                                    script$, _
                                    "HTTP/1.0", _
                                    vbNullString, _
                                    0, _
                                    INTERNET_FLAG_RELOAD, _
                                    0)

                If hHttpOpenRequest <> 0 Then
                    Dim sHeader As String
                    Const HTTP_ADDREQ_FLAG_ADD = &H20000000
                    Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000
                    sHeader = "Content-Type: application/x-www-form-urlencoded" _
                               & vbCrLf
                    bRet = HttpAddRequestHeaders(hHttpOpenRequest, _
                      sHeader, Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE _
                      Or HTTP_ADDREQ_FLAG_ADD)

                    Dim lpszPostData As String
                    Dim lPostDataLen As Long

                    lpszPostData = postdat$
                    lPostDataLen = Len(lpszPostData)
                    bRet = HttpSendRequest(hHttpOpenRequest, _
                           vbNullString, _
                           0, _
                            lpszPostData, _
                           lPostDataLen)

                    Dim bDoLoop As Boolean
                    'Dim sReadBuffer As String * 2048
                    Dim sReadBuffer As String = 2048

                    Dim lNumberOfBytesRead As Long
                    Dim sBuffer As String = 0
                    bDoLoop = True

                    While bDoLoop
                        sReadBuffer = vbNullString
                        bDoLoop = InternetReadFile(hHttpOpenRequest, _
                           sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)

                        sBuffer = sBuffer & _
                             Left(sReadBuffer, lNumberOfBytesRead)
                        If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
                    End While
                    PostInfo = sBuffer
                    bRet = InternetCloseHandle(hHttpOpenRequest)
                End If
                bRet = InternetCloseHandle(hInternetConnect)
            End If
            bRet = InternetCloseHandle(hInternetOpen)
        End If
    End Function

    Public Sub SplitAddr(ByVal addr$, ByVal srv$, ByVal script$)
        'Inputs: The full url including http://
        ' Two variables that will be changed
        '
        'Returns: Splits the addr$ var into the server name
        ' and the script path

        Dim i%

        i = InStr(addr$, "/")
        srv$ = Mid(addr$, i + 2, Len(addr$) - (i + 1))
        i = InStr(srv$, "/")
        script$ = Mid(srv$, i, Len(srv$) + 1 - i)
        srv$ = Left$(srv$, i - 1)

        'MsgBox srv & "  Server " & script

    End Sub

End Module
 
Most likely because declarations are wrong. You should be aware that VB6 and VB.Net does not have same data types, see Language Changes for Visual Basic 6.0 Users.
You might also be interested in knowing that .Net library has lots of tools built in for networking like WebClient, WebRequest and various Socket related classes.
 
How to use vb.net to get a Web Page

Solved ! How to use vb.net to get a Web Page

VB.NET:
Imports System.Net
Imports System.IO

Public Class Form3

    'Using HttpWebRequest and HttpWebResponse
    Public Function getHTMLSource1(ByVal url As String) As String
        Dim HTMLSource As String = ""
        Dim Request As HttpWebRequest = WebRequest.Create(url)
        Dim Response As HttpWebResponse = Request.GetResponse
        Dim SR As StreamReader
        SR = New StreamReader(Response.GetResponseStream)
        HTMLSource = SR.ReadToEnd
        SR.Close()
        Return HTMLSource
    End Function

    'Using Webclient
    Public Function getHTMLSource2(ByVal url As String) As String
        Dim HTMLSource As String = ""
        Dim WC As New WebClient
        Dim SR As StreamReader
        SR = New StreamReader(WC.OpenRead(url))
        HTMLSource = SR.ReadToEnd
        Return HTMLSource
    End Function
    'Using WebRequest WebResponse
    Public Function getHTMLSource3(ByVal url As String) As String
        Dim HTMLSource As String = ""
        Dim Request As WebRequest = WebRequest.Create(url)
        Dim Response As WebResponse = Request.GetResponse
        Dim SR As StreamReader
        SR = New StreamReader(Response.GetResponseStream)
        HTMLSource = SR.ReadToEnd
        SR.Close()
        Return HTMLSource
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim out1 As String
        out1 = getHTMLSource1("http://host.domain.com/folder/auth.php?user=vbdotnet&pass=123&submit=submit")
        MsgBox(out1)
    End Sub
End Class

Reference link:
http://madarong.com/Blog.php/Blog/5...HTTPWebResponse,--WebClient-to-Get-a-Web-Page
 
Back
Top