fb:porticula NoPaste
[BugWare - NONTESTED] httpget/post mit Proxy
Uploader: | PMedia |
Datum/Zeit: | 08.01.2008 20:40:46 |
#ifdef __FB_WIN32__
#include once "win/winsock2.bi"
#else
#include once "crt/netdb.bi"
#include once "crt/sys/socket.bi"
#include once "crt/netinet/in.bi"
#include once "crt/arpa/inet.bi"
#include once "crt/unistd.bi"
#endif
#ifndef recvbufflen
#define RECVBUFFLEN 16384
#endif
#ifndef newline
#define newline chr(13,10)
#endif
Dim Shared Proxy As String
Sub InitWinsock Constructor
#ifdef __FB_WIN32__
'' init winsock
Dim wsaData As WSAData
If( WSAStartup( MAKEWORD( 1, 1 ), @wsaData ) <> 0 ) Then
Print "Error: WSAStartup failed"
End 1
End If
#Endif
End Sub
Sub ExitWinsock Destructor
#ifdef __FB_WIN32__
WSACleanup
#Endif
End Sub
Function httpget(server As String, path As String, hadd as string = "") As String
Dim IP As Integer
Dim ia As in_addr
Dim s As SOCKET
Dim hostentry As hostent Ptr
Dim sendbuffer As String
Dim recvbuffer As Zstring * RECVBUFFLEN+1
Dim bytes As Integer
Dim sa As sockaddr_in
Dim in as string
ia.S_addr = inet_addr( proxy )
If ( ia.S_addr = INADDR_NONE ) Then
hostentry = gethostbyname( proxy )
If ( hostentry = 0 ) Then
return "IP couldn't be resolved!"
End If
IP = *cast( Integer Ptr, *hostentry->h_addr_list )
Else
IP = ia.S_addr
End If
s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
If( s = 0 ) Then
return "Socket couldn't be opened."
End If
sa.sin_port = htons( 80 )
sa.sin_family = AF_INET
sa.sin_addr.S_addr = ip
If ( connect( s, cast( PSOCKADDR, @sa ), Len( sa )) = SOCKET_ERROR ) Then
closesocket( s )
return "Couldn't connect to host"
End If
sendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _
"Host: " + server + NEWLINE + _
"Connection: close" + NEWLINE + _
hadd + _
NEWLINE
If( send( s, sendBuffer, Len( sendBuffer ), 0 ) = SOCKET_ERROR ) Then
closesocket( s )
return "Couldn't send request"
End If
Do
bytes = recv( s, recvBuffer, RECVBUFFLEN, 0 )
If( bytes <= 0 ) Then
exit do
End If
recvbuffer[bytes] = 0
in += recvbuffer
Loop
shutdown( s, 2 )
closesocket( s )
return in
End Function
Function httppost(server As String, path As String, toPost As String, hadd as string = "") As String
Dim IP As Integer
Dim ia As in_addr
Dim s As SOCKET
Dim hostentry As hostent Ptr
Dim sendbuffer As String
Dim recvbuffer As Zstring * RECVBUFFLEN+1
Dim bytes As Integer
Dim sa As sockaddr_in
Dim in as string
ia.S_addr = inet_addr( proxy )
If ( ia.S_addr = INADDR_NONE ) Then
hostentry = gethostbyname( proxy )
If ( hostentry = 0 ) Then
return "IP couldn't be resolved!"
End If
IP = *cast( Integer Ptr, *hostentry->h_addr_list )
Else
IP = ia.S_addr
End If
s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
If( s = 0 ) Then
return "Socket couldn't be opened."
End If
sa.sin_port = htons( 80 )
sa.sin_family = AF_INET
sa.sin_addr.S_addr = ip
If ( connect( s, cast( PSOCKADDR, @sa ), Len( sa )) = SOCKET_ERROR ) Then
closesocket( s )
return "Couldn't connect to host"
End If
sendBuffer = "POST /" + path + " HTTP/1.0" + NEWLINE + _
"Host: " + server + NEWLINE + _
"Content-Type: application/x-www-form-urlencoded" + NEWLINE + _
"Content-Length: " + str(len(toPost)) + NEWLINE + _
"Connection: close" + NEWLINE + _
hadd + _
NEWLINE + _
toPost + NEWLINE
If( send( s, sendBuffer, Len( sendBuffer ), 0 ) = SOCKET_ERROR ) Then
closesocket( s )
return "Couldn't send request"
End If
Do
bytes = recv( s, recvBuffer, RECVBUFFLEN, 0 )
If( bytes <= 0 ) Then
exit do
End If
recvbuffer[bytes] = 0
in += recvbuffer
Loop
shutdown( s, 2 )
closesocket( s )
return in
End Function