InternetReadFile error

G

Guest

Hello,

I've looked through several newsgroups and have yet to find help with this
problem. We have a device we can access through IE, using a URL, that
returns an XML string.

For example, typing
"http://192.168.1.254/cgi-bin/dataProxy?oper=queryEvents&raw=1" into IE
returns this XML String:
<?xml version="1.0" ?>
- <Matrics>
- <EventGroup>
- <EventList>
<Tag event="1" raw="03080507A80200100009415D" time="41dd2180" RPL="1" />
<Tag raw="03080507A802001000257310" time="41dd170b" />
</EventList>
</EventGroup>
</Matrics>

In VB6 I got this to work, as a test, using a form and the following
subroutine:

Private Sub QueryReader(strIPAddress As String, intPort As Integer,
strFormat As String)
On Error GoTo Err_QueryReader

Dim hInternet As Long
Dim hConnect As Long
Dim hRequest As Long
Dim bRes As Boolean
Dim lBytesRead As Long
Dim strURL As String, strData As String
Dim x As Long

hInternet = InternetOpen("Test", INTERNET_OPEN_TYPE_PRECONFIG,
vbNullString, vbNullString, 0)
hConnect = InternetConnect(hInternet, strIPAddress, intPort, "", "",
INTERNET_SERVICE_HTTP, 0, 0)

strURL = "/cgi-bin/dataProxy?oper=queryEvents"
If strFormat = "Raw" Then strURL = strURL & "&raw=1"


hRequest = HttpOpenRequest(hConnect, "GET", strURL, vbNullString,
vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bRes = HttpSendRequest(hRequest, vbNullString, 0, 0, 0)

' If request was valid, get the data
If CBool(bRes) Then
Dim sBuffer As String * 65000
sBuffer = ""
bRes = InternetReadFile(hRequest, sBuffer, Len(sBuffer), lBytesRead)
If lBytesRead > 0 Then

' Store inventory History record and the associated part record
Dim cmd As New ADODB.Command
Dim prm As New ADODB.Parameter
Set cmd = Nothing
Set cmd.ActiveConnection = cnn
With cmd
.CommandText = "UpdateRFIDInformation"
.CommandType = adCmdStoredProc

strData = Trim(sBuffer)
strData = Replace(strData, "<?xml version='1.0'?>", "")
strData = Replace(strData, "'", Chr(34))

Set prm = .CreateParameter("data", adVarChar, adParamInput,
65000, strData)
.Parameters.Append prm

Set prm = .CreateParameter("ip", adVarChar, adParamInput,
15, strIPAddress)
.Parameters.Append prm
.Execute
End With
End If
End If
bRes = InternetCloseHandle(hInternet)

Exit_QueryReader:
Set cmd = Nothing
Set prm = Nothing
Exit Sub

Err_QueryReader:
MsgBox Err.Description
GoTo Exit_QueryReader

End Sub


SO then I wanted to use VB.NET to create this as a system service but the
call to InternetReadFile never returns anything in the buffer. I'm assuming
it might have something to do with my function declaration. I've tried
numerous things and still come up with nothing. Any help would be
appreciated. The .NET code is below:



Public Declare Auto Function InternetReadFile Lib "wininet.dll" _
(ByVal hFile As Long, ByVal sBuffer As System.Text.StringBuilder, _
ByVal lNumberOfBytesToRead As Long, ByRef lNumberOfBytesRead As
Long) As Long


Sub QueryReader(ByVal strIPAddress As String, ByVal intPort As Int32,
ByVal strFormat As String)

Dim hInternet As Long
Dim hConnect As Long
Dim hRequest As Long
Dim bRes As Long
Dim lBytesRead As Long
Dim strURL As String, strData As String
Dim Buffer As New System.Text.StringBuilder(BufLen)
Dim nSize As Integer = Buffer.Capacity


Try

hInternet = InternetOpen("RFID Monitor",
INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
hConnect = InternetConnect(hInternet, strIPAddress, intPort, "",
"", INTERNET_SERVICE_HTTP, 0, 0)

strURL = "/cgi-bin/dataProxy?oper=queryEvents"
If strFormat = "Raw" Then strURL = strURL & "&raw=1"

hRequest = HttpOpenRequest(hConnect, "GET", strURL,
vbNullString, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bRes = HttpSendRequest(hRequest, vbNullString, 0, 0, 0)

' If request was valid, get the data
If bRes Then

bRes = InternetReadFile(hRequest, Buffer, nSize, lBytesRead)
Console.WriteLine(bRes)

Dim myEx As New Win32Exception(bRes)
Console.WriteLine(myEx.Message)
Console.WriteLine(myEx.ErrorCode)
Console.WriteLine(myEx.HelpLink)
Console.WriteLine(myEx.Source)

If lBytesRead > 0 Then
Dim cmd As New
SqlClient.SqlCommand("UpdateRFIDInformation", AT)
cmd.CommandType = CommandType.StoredProcedure
With cmd
strData.Trim()
strData.Replace("<?xml version='1.0'?>", "")
strData.Replace("'", Chr(34))

.Parameters.Add("@xml", strData)
.Parameters.Add("@IPAddress", strIPAddress)
.ExecuteNonQuery()
End With
End If

End If

Catch ex As Exception
EventLog1.WriteEntry(ex.Message)


Finally
bRes = InternetCloseHandle(hInternet)

End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top