Ich verwende aktuell folgenden Code (aus Forum und PureArea) um Content (genaugenommen HTTP Ausgaben eines PHP Scriptes) vom Internet auszulesen. Das charmante an diesem Code ist, dass es die Einstellungen (Proxy etc.) und auch SSL für HTTPS alles von selbst erledigt.
Jetzt sollte das ganze aber nach Linux portiert werden. Hat jemand eine Idee wie ich eine solche Funktion unter Linux erhalten kann? Ich habe leider keine Ahnung von Linux API...
Hier mein Windows-Code:
Code: Alles auswählen
#INTERNET_OPEN_TYPE_DIRECT = 1
#INTERNET_FLAG_SECURE = $800000 ;1
#INTERNET_FLAG_NO_CACHE_WRITE = $04000000
#INTERNET_FLAG_RELOAD = $80000000
#INTERNET_OPEN_TYPE_PRECONFIG = 0
Global InternetHandle.l
Procedure.s GetLastInternetError()
Protected lErr.l
Protected sErr.s
Protected lenBuf.l
lErr.l = 0
sErr.s = ""
lenBuf.l = 0
;get the required buffer size
InternetGetLastResponseInfo_(lErr.l, sErr.s, lenBuf.l)
;create a buffer
sErr.s = Space(lenBuf.l)
;retrieve the last respons info
InternetGetLastResponseInfo_(lErr.l, sErr.s, lenBuf.l)
ProcedureReturn Left(sErr.s, lenBuf.l) + " (" + Str(lErr.l) + ")"
EndProcedure
Procedure.s CreateConnection()
; Create an internet connection
; Returns "" if success, otherwise the error...
hOpen.l = InternetOpen_("agent description", #INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0)
If hOpen.l = 0
InternetHandle.l = 0
ProcedureReturn "ERROR: " + GetLastInternetError()
Else
InternetHandle.l = hOpen.l
ProcedureReturn ""
EndIf
EndProcedure
Procedure CloseConnection()
If InternetHandle.l <> 0
InternetCloseHandle_(InternetHandle.l)
EndIf
EndProcedure
Procedure.s ReadURL(sURL.s, UseSecure.b)
Protected sBuffer.s
Protected Ret.l
Protected hOpen.l
Protected hFile.l
Protected Result.s
;
Debug "Internet: " + sURL.s
sBuffer.s = Space(32768) ; Transmission-buffer is 32KB
;Open the url
If UseSecure.b = #True
hFile.l = InternetOpenUrl_(InternetHandle.l, sURL.s, "", 0, #INTERNET_FLAG_RELOAD + #INTERNET_FLAG_SECURE, 0)
Else
hFile.l = InternetOpenUrl_(InternetHandle.l, sURL.s, "", 0, #INTERNET_FLAG_RELOAD , 0)
EndIf
If hFile.l = 0: ProcedureReturn "ERROR: " + GetLastInternetError(): EndIf
;Read the first bytes of the file
Ret.l = 0
Erg.b = InternetReadFile_(hFile.l, @sBuffer.s, Len(sBuffer.s), @Ret.l)
If Erg.b = #True
Result.s = Left(sBuffer.s,Ret.l)
While Ret.l = Len(sBuffer.s)
; read more because buffer is full...
InternetReadFile_(hFile.l, @sBuffer.s, Len(sBuffer.s), @Ret.l)
Result.s = Left(sBuffer.s,Ret.l)
Result.s = Result.s + Left(sBuffer.s,Ret.l)
Wend
; find the beginning of the POST data...
Pos.l = FindString(Result.s, "<body>", 1)
If Pos.l > 0
Result.s = Mid(Result.s, Pos.l + 6,Len(Result.s) - Pos.l)
While Left(Result.s, 1) = #CR$ Or Left(Result.s, 1) = #LF$
Result.s = Mid(Result.s, 2, Len(Result.s) - 1)
Wend
EndIf
Else
Result.s = "ERROR: " + GetLastInternetError()
EndIf
;clean up
InternetCloseHandle_(hFile.l)
If Left(Result.s, 4) = Chr($FF)+Chr($FE)+Chr(0)+Chr(0) Or Left(Result.s, 4) = Chr(0)+Chr(0)+Chr($FE)+Chr($FF)
; UTF-32
Debug "UTF-32!"
Result.s = Mid(Result.s, 5, Len(Result.s) - 4)
EndIf
If Left(Result.s, 2) = Chr($FF)+Chr($FE) Or Left(Result.s, 4) = Chr($FE)+Chr($FF)
; UTF-16
Debug "UTF-16!"
Result.s = Mid(Result.s, 3, Len(Result.s) - 2)
EndIf
If Left(Result.s, 3) = Chr($EF)+Chr($BB)+Chr($BF)
; UTF-8
Debug "UTF-8!"
Result.s = Mid(Result.s, 4, Len(Result.s) - 3)
EndIf
Debug "Internet Result: " + Result.s
ProcedureReturn Result.s
EndProcedure
Code: Alles auswählen
Result.s = CreateConnection()
If Result.s = ""
; Seite mit normalem HTTP
URL.s = "http://www.test.com/seite.php?parameter=1"
Result.s = ReadURL(URL.s, #False)
Debug "Seite normal: " + Result.s
; Seite über HTTPS
URL.s = "https://www.test.com/seite.php?parameter=1"
Result.s = ReadURL(URL.s, #True)
Debug "Seite https: " + Result.s
CloseConnection()
Else
Debug "Error: " + Result.s
End If
Volker