Re: Envoi de fichier par requête Http Post
Publié : dim. 26/juin/2011 16:18
haaaaaaa peut-etre que je sais attendez 2 min ^^
Forums PureBasic - Français
https://www.purebasic.fr/french/
Code : Tout sélectionner
;*********************************************
;***** Envoi de fichier par requete HTTP *****
;******** Par lepiaf31 le 26/06/2011 *********
;*********************************************
URL.s = "www.thyphoon.com"
PATH.s = "/test.php"
ACTION.s = "datafile"
BOUNDARY.s = "-----------------------------306262331012750"
POST.s = ""
FILE.s = OpenFileRequester("Please choose file to load", "", "*.*", 0)
OpenConsole()
If InitNetwork()
PrintN("Network initialised ...")
connection = OpenNetworkConnection(URL, 80)
If connection
PrintN("Connection opened ...")
f = OpenFile(#PB_Any, FILE)
If f
fileHeader.s = "Content-Disposition: form-data; name="+Chr(34)+ACTION+Chr(34) +"; filename="+Chr(34)+FILE+Chr(34)+#CRLF$
fileHeader + "Content-Type: application/octet-stream" + #CRLF$ + #CRLF$
POST = "POST "+PATH+" HTTP/1.0"+ #CRLF$
POST + "Host: "+URL + #CRLF$
POST + "Connection: keep-alive" + #CRLF$
POST + "Content-Type: multipart/form-data; boundary="+BOUNDARY + #CRLF$
POST + "Content-Length: "+Str(FileSize(FILE)+2*Len(BOUNDARY)+Len(fileHeader)+10) + #CRLF$
POST + #CRLF$
POST + "--"+BOUNDARY+#CRLF$+fileHeader
SendNetworkData(connection, @POST, Len(POST))
Debug POST
*buffer = AllocateMemory(2048)
While Eof(f) = 0
readed = ReadData(f, *buffer, 2048)
SendNetworkData(connection, *buffer, readed)
Wend
POST = #CRLF$ + "--" + BOUNDARY + "--"
SendNetworkData(connection, @POST, Len(POST))
FreeMemory(*buffer)
CloseFile(f)
string.s = Space(2048)
time = ElapsedMilliseconds()
Repeat
If NetworkClientEvent(Connection) = #PB_NetworkEvent_Data
readed = ReceiveNetworkData(connection, @string, 2048)
PrintN(Left(string, readed))
time = ElapsedMilliseconds()
EndIf
Delay(100)
Until ElapsedMilliseconds() - time >= 3000
Else
PrintN("File not found")
EndIf
Else
PrintN("Impossible to etablished the connection")
EndIf
Else
PrintN("InitNetwok problem")
EndIf
Input()
ça marche du feu de dieu !lepiaf31 a écrit :C'est bon voilà un code qui pète le feu:
Code : Tout sélectionner
Structure post_variable
name.s
value.s
EndStructure
Structure post_file
actionName.s ; this is important!! this action must be the same as <form ... name="file">
path.s
mime.s
EndStructure
Structure post_structure
url.s
List variable.post_variable()
List file.post_file()
EndStructure
Procedure setHttpPostUrl(*post.post_structure,url.s)
*post\url=url
EndProcedure
Procedure addHttpPostfiles(*post.post_structure,file.s,actionName.s="file")
If FileSize(file)>0
AddElement(*post\file())
*post\file()\path=file
*post\file()\actionName=actionName
*post\file()\mime="text/plain";GetMimeType(GetExtensionPart(file))
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure sendHttpFile(*post.post_structure)
url.s = GetURLPart(*post\url, #PB_URL_Site); the main domain
port.l= Val(GetURLPart(*post\url, #PB_URL_Port))
If port=0:port=80:EndIf
;-Send Variable
ForEach *post\variable()
Next
;-Send File
ForEach *post\file()
Next
If InitNetwork()
cnx = OpenNetworkConnection(url,80)
If cnx
Else
Debug "NO CONNECTION"
EndIf
EndIf
EndProcedure
post.post_structure
setHttpPostUrl(@post,"http://127.0.0.1/test.php")
addHttpPostfiles(@post,"C:\test.txt")
sendHttpFile(@post)
merci c'est sympa ! je vais quand même essayer de mon côtélepiaf31 a écrit :Heu ouep je regarde ca un peu plus tard là jsuis un peu occupé (je mate un live de SC2 pour ceux qui connaissent)
Code : Tout sélectionner
Structure post_variable
name.s ;Variable Name
value.s ;Variable Value
EndStructure
Structure post_file
actionName.s ; this is important!! this action must be the same as <form ... name="file">
path.s ; path+filename about file to send
mime.s ;mime-type
EndStructure
Structure post_structure
url.s ;url to send POST
List variable.post_variable() ;variables
List file.post_file() ;files
EndStructure
;Init Url to send Post Data
Procedure setHttpPostUrl(*posts.post_structure,url.s)
*posts\url=url
EndProcedure
;add a variable to the Post Data List
Procedure addHttpPostVariable(*posts.post_structure,name.s,value.s)
AddElement(*posts\variable())
*posts\variable()\name=name
*posts\variable()\value=value
EndProcedure
;add a file to the Post Data List
Procedure addHttpPostfiles(*posts.post_structure,file.s,actionName.s="file")
If FileSize(file)>0
AddElement(*posts\file())
*posts\file()\path=file
*posts\file()\actionName=actionName
*posts\file()\mime="text/plain";GetMimeType(GetExtensionPart(file))
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
;Send Data List to the url
Procedure.s sendHttpDatas(*posts.post_structure)
Protected url.s,path.s,port.l,connection.i,f.i,POST.s,BOUNDARY.s
url = GetURLPart(*posts\url, #PB_URL_Site); the main domain
path =GetURLPart(*posts\url,#PB_URL_Path); the path
port= Val(GetURLPart(*posts\url, #PB_URL_Port))
If port=0:port=80:EndIf
POST = "POST "+"/"+path+" HTTP/1.0"+ #CRLF$
POST + "Host: "+url + #CRLF$
POST + "Connection: keep-alive" + #CRLF$
BOUNDARY = "-----------------------------306262331012750"
connection = OpenNetworkConnection(URL, port)
If connection
Debug "Connection opened ..."
;-Send Variable
ForEach *posts\variable()
Debug "send variable:"+*posts\variable()\name+"="+*posts\variable()\value
;-TODO
Next
;-Send File
ForEach *posts\file()
f = OpenFile(#PB_Any, *posts\file()\path)
If f
Debug "send file:"+GetFilePart(*posts\file()\path)
fileHeader.s = "Content-Disposition: form-data; name="+Chr(34)+*posts\file()\actionName+Chr(34) +"; filename="+Chr(34)+*posts\file()\path+Chr(34)+#CRLF$
fileHeader + "Content-Type: application/octet-stream" + #CRLF$ + #CRLF$
POST + "Content-Type: multipart/form-data; boundary="+BOUNDARY + #CRLF$
POST + "Content-Length: "+Str(FileSize(*posts\file()\path)+2*Len(BOUNDARY)+Len(fileHeader)+10) + #CRLF$
POST + #CRLF$
POST + "--"+BOUNDARY+#CRLF$+fileHeader
SendNetworkData(connection, @POST, Len(POST))
*buffer = AllocateMemory(2048)
While Eof(f) = 0
readed = ReadData(f, *buffer, 2048)
SendNetworkData(connection, *buffer, readed)
Wend
POST = #CRLF$ + "--" + BOUNDARY + "--"
SendNetworkData(connection, @POST, Len(POST))
FreeMemory(*buffer)
CloseFile(f)
Else
MessageRequester("Error","File No Found :"+#CRLF$+*posts\file()\path,#PB_MessageRequester_Ok)
EndIf
POST="";reset variablefor next loop
Next
;-server Answer
string.s = Space(2048)
result.s=""
Repeat
readed = ReceiveNetworkData(connection, @string, 2048)
result+Left(string, readed)
Delay(100)
Until readed < 2048
Debug"____"
ProcedureReturn result
Else
Debug "NO CONNECTION"
EndIf
EndProcedure
InitNetwork()
test.post_structure
setHttpPostUrl(@test,"http://www.thyphoon.com/test.php")
addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileA")
addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileB")
MessageRequester("result",sendHttpDatas(@test),#PB_MessageRequester_Ok)
Code : Tout sélectionner
Repeat
readed = ReceiveNetworkData(connection, @string, 2048)
result+Left(string, readed)
Delay(100)
Until readed < 2048
Code : Tout sélectionner
time = elapsedmilliseconds()
Repeat
time = ElapsedMilliseconds()
Repeat
If NetworkClientEvent(Connection) = #PB_NetworkEvent_Data
readed = ReceiveNetworkData(connection, @string, 2048)
result + left-string, readed)
time = ElapsedMilliseconds()
EndIf
Delay(100)
Until ElapsedMilliseconds() - time >= 3000
ba merci de pensé a moi pendant pa pause pub lollllllepiaf31 a écrit :Je n'ai pas vraiment regardé ton code (la finale de SC2 n'est toujours pas finie :p)
Effectivement tu as raison du coup j'arrive de nouveau a envoyer un fichier, et du coup je comprends mieux l’intérêt d'un code vu sur le forum anglaislepiaf31 a écrit : Car parfois il arrive que le serveur mette du temps à répondre et du coup le programme s'arrete trop vite (il n'existe malheureusement pas de fonction en PB natif pour savoir quand le connexion se termine). Du coup je conseille plutot de mettre un timeout à 3 secondes pour être sûr de recevoir toute la page HTML.
Je veux bien, je n'ai pas encore réussit a envoyer 2 fichiers en même temps , ni a rajouter des variables !lepiaf31 a écrit : JE regarderai avec plus d'attention un peu plus tard si je peux
oui j'aimerais réussir a avoir un code cross-plateforme vu que j'en aurais besoin sous linux, windows, et MacOslepiaf31 a écrit :Oui en effet je préfère aussi utilisé les sockets mais je ne les utilise que sous Windows donc je ne sais pas si tu cherches du code portable ou pas ?
Code : Tout sélectionner
Structure post_variable
name.s ;Variable Name
value.s ;Variable Value
EndStructure
Structure post_file
actionName.s ; this is important!! this action must be the same as <form ... name="file">
path.s ; path+filename about file to send
mime.s ; mime-type
EndStructure
Structure proxy_structure
server.s ;
port.l ;
login.s ;
password.s ;
EndStructure
Structure post_structure
url.s ;url to send POST
proxy.proxy_structure
List variable.post_variable() ;variables
List file.post_file() ;files
EndStructure
;Init Url to send Post Data
Procedure setProxy(*posts.post_structure,server.s,port.l,login.s,password.s)
*posts\proxy\server=server
*posts\proxy\port=port
*posts\proxy\login=login
*posts\proxy\password=password
EndProcedure
Procedure setHttpPostUrl(*posts.post_structure,url.s)
*posts\url=url
EndProcedure
;add a variable to the Post Data List
Procedure addHttpPostVariable(*posts.post_structure,name.s,value.s)
AddElement(*posts\variable())
*posts\variable()\name=name
*posts\variable()\value=value
EndProcedure
;add a file to the Post Data List
Procedure addHttpPostfiles(*posts.post_structure,file.s,actionName.s="file")
If FileSize(file)>0
AddElement(*posts\file())
*posts\file()\path=file
*posts\file()\actionName=actionName
*posts\file()\mime="text/plain";GetMimeType(GetExtensionPart(file))
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
;Send Data List to the url
Procedure.s sendHttpDatas(*posts.post_structure)
Protected url.s,path.s,port.l,connection.i,f.i,POST.s,BOUNDARY.s
url = GetURLPart(*posts\url, #PB_URL_Site); the main domain
path =GetURLPart(*posts\url,#PB_URL_Path); the path
port= Val(GetURLPart(*posts\url, #PB_URL_Port))
If port=0:port=80:EndIf
POST=""
POST + "POST "+"/"+path+" HTTP/1.0"+ #CRLF$
;-TEST SI PROXY
If *posts\proxy\server<>""
conc$=*posts\proxy\login+":"+*posts\proxy\password
*OutputBuffer = AllocateMemory(Len(conc$)*4)
Base64Encoder(@conc$,Len(conc$),*OutputBuffer,Len(conc$)*4)
enc$=PeekS(*OutputBuffer)
FreeMemory(*OutputBuffer)
POST +"Proxy-Authorization: Basic "+enc$+#CRLF$
EndIf
POST + "Host: "+url + #CRLF$
POST + "Connection: keep-alive" + #CRLF$
BOUNDARY = "-----------------------------306262331012750" ;http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
connection = OpenNetworkConnection(URL, port)
If connection
Debug "Connection opened ..."
;-Send Variable
ForEach *posts\variable()
Debug "send variable:"+*posts\variable()\name+"="+*posts\variable()\value
;-TODO
Next
;-Send File
ForEach *posts\file()
f = OpenFile(#PB_Any, *posts\file()\path)
If f
Debug "send file:"+GetFilePart(*posts\file()\path)
fileHeader.s = "Content-Disposition: form-data; name="+Chr(34)+*posts\file()\actionName+Chr(34) +"; filename="+Chr(34)+*posts\file()\path+Chr(34)+#CRLF$
fileHeader + "Content-Type: application/octet-stream" + #CRLF$ + #CRLF$
POST + "Content-Type: multipart/form-data; boundary="+BOUNDARY + #CRLF$
POST + "Content-Length: "+Str(FileSize(*posts\file()\path)+2*Len(BOUNDARY)+Len(fileHeader)+10) + #CRLF$
POST + #CRLF$
POST + "--"+BOUNDARY+#CRLF$+fileHeader
Debug POST
SendNetworkData(connection, @POST, Len(POST))
*buffer = AllocateMemory(2048)
While Eof(f) = 0
readed = ReadData(f, *buffer, 2048)
SendNetworkData(connection, *buffer, readed)
Wend
POST = #CRLF$ + "--" + BOUNDARY + "--"
SendNetworkData(connection, @POST, Len(POST))
FreeMemory(*buffer)
CloseFile(f)
Else
MessageRequester("Error","File No Found :"+#CRLF$+*posts\file()\path,#PB_MessageRequester_Ok)
EndIf
POST="";reset variablefor next loop
Next
;-server Answer
string.s = Space(2048)
result.s=""
time = ElapsedMilliseconds()
Repeat
If NetworkClientEvent(Connection) = #PB_NetworkEvent_Data
readed = ReceiveNetworkData(connection, @string, 2048)
result + Left(string, readed)
time = ElapsedMilliseconds()
EndIf
Delay(100)
Until ElapsedMilliseconds() - time >= 3000
Debug"____"
ProcedureReturn result
Else
Debug "NO CONNECTION"
EndIf
EndProcedure
InitNetwork()
test.post_structure
setHttpPostUrl(@test,"http://www.thyphoon.com/test.php")
addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileA")
;addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileB")
MessageRequester("result",sendHttpDatas(@test),#PB_MessageRequester_Ok)
Code : Tout sélectionner
Structure post_variable
name.s ;Variable Name
value.s ;Variable Value
EndStructure
Structure post_file
actionName.s ; this is important!! this action must be the same as <form ... name="file">
path.s ; path+filename about file to send
mime.s ; mime-type
EndStructure
Structure proxy_structure
server.s ;
port.l ;
login.s ;
password.s ;
EndStructure
Structure post_structure
url.s ;url to send POST
proxy.proxy_structure
List variable.post_variable() ;variables
List file.post_file() ;files
EndStructure
;Init Url to send Post Data
Procedure setProxy(*posts.post_structure,server.s,port.l,login.s="",password.s="")
*posts\proxy\server=server
*posts\proxy\port=port
*posts\proxy\login=login
*posts\proxy\password=password
EndProcedure
Procedure setHttpPostUrl(*posts.post_structure,url.s)
*posts\url=url
EndProcedure
;add a variable to the Post Data List
Procedure addHttpPostVariable(*posts.post_structure,name.s,value.s)
AddElement(*posts\variable())
*posts\variable()\name=name
*posts\variable()\value=value
EndProcedure
;add a file to the Post Data List
Procedure addHttpPostfiles(*posts.post_structure,file.s,actionName.s="file")
If FileSize(file)>0
AddElement(*posts\file())
*posts\file()\path=file
*posts\file()\actionName=actionName
*posts\file()\mime="text/plain";GetMimeType(GetExtensionPart(file))
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
;Send Data List to the url
Procedure.s sendHttpDatas(*posts.post_structure)
Protected url.s,path.s,port.l,connection.i,f.i,POST.s,BOUNDARY.s
url = GetURLPart(*posts\url, #PB_URL_Site); the main domain
path =GetURLPart(*posts\url,#PB_URL_Path); the path
port= Val(GetURLPart(*posts\url, #PB_URL_Port))
If port=0:port=80:EndIf
POST=""
;-TEST SI PROXY
;http://www.purebasic.fr/english/viewtopic.php?f=12&t=10327
If *posts\proxy\server<>""
connection = OpenNetworkConnection(*posts\proxy\server, *posts\proxy\port)
POST + "POST "+*posts\url+" HTTP/1.0"+ #CRLF$
If *posts\proxy\login<>""
conc$=*posts\proxy\login+":"+*posts\proxy\password
*OutputBuffer = AllocateMemory(Len(conc$)*4)
Base64Encoder(@conc$,Len(conc$),*OutputBuffer,Len(conc$)*4)
enc$=PeekS(*OutputBuffer)
FreeMemory(*OutputBuffer)
POST +"Proxy-Authorization: Basic "+enc$+#CRLF$
EndIf
Else
connection = OpenNetworkConnection(url, port)
POST + "POST "+"/"+path+" HTTP/1.0"+ #CRLF$
EndIf
POST + "Host: "+url + #CRLF$
;POST + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7" + #CRLF$
;si il y a un acces proteger avec htaccess
;POST + "Authorization: Basic "+ OutputBuffer + #CRLF$
POST + "Connection: keep-alive" + #CRLF$
BOUNDARY = "-----------------------------306262331012750" ;http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
If connection
Debug "Connection opened ..."
;-Send Variable
;http://www.purebasic.fr/english/viewtopic.php?f=13&t=13759
If ListSize(*posts\variable())
variable.s=""
ForEach *posts\variable()
Debug "send variable:"+*posts\variable()\name+"="+*posts\variable()\value
If ListIndex(*posts\variable())>0
variable+"&"
EndIf
variable+*posts\variable()\name+"="+*posts\variable()\value
;-TODO
Next
POST + "Cache-Control: max-age=0" + Chr(13) + Chr(10)
POST + "Content-Type: application/x-www-form-urlencoded" + Chr(13) + Chr(10)
POST + "Content-Length: " + Str(Len(variable)) + Chr(13) + Chr(10)
POST + Chr(13) + Chr(10)
POST + post$
EndIf
;-Send File
ForEach *posts\file()
f = OpenFile(#PB_Any, *posts\file()\path)
If f
Debug "send file:"+GetFilePart(*posts\file()\path)
fileHeader.s = "Content-Disposition: form-data; name="+Chr(34)+*posts\file()\actionName+Chr(34) +"; filename="+Chr(34)+*posts\file()\path+Chr(34)+#CRLF$
fileHeader + "Content-Type: application/octet-stream" + #CRLF$ + #CRLF$
POST + "Content-Type: multipart/form-data; boundary="+BOUNDARY + #CRLF$
POST + "Content-Length: "+Str(FileSize(*posts\file()\path)+2*Len(BOUNDARY)+Len(fileHeader)+10) + #CRLF$
POST + #CRLF$
POST + "--"+BOUNDARY+#CRLF$+fileHeader
Debug POST
SendNetworkData(connection, @POST, Len(POST))
*buffer = AllocateMemory(2048)
While Eof(f) = 0
readed = ReadData(f, *buffer, 2048)
SendNetworkData(connection, *buffer, readed)
Wend
POST = #CRLF$ + "--" + BOUNDARY + "--"
SendNetworkData(connection, @POST, Len(POST))
FreeMemory(*buffer)
CloseFile(f)
Else
MessageRequester("Error","File No Found :"+#CRLF$+*posts\file()\path,#PB_MessageRequester_Ok)
EndIf
POST="";reset variablefor next loop
Next
;-server Answer
string.s = Space(2048)
result.s=""
time = ElapsedMilliseconds()
Repeat
If NetworkClientEvent(Connection) = #PB_NetworkEvent_Data
readed = ReceiveNetworkData(connection, @string, 2048)
result + Left(string, readed)
time = ElapsedMilliseconds()
EndIf
Delay(100)
Until ElapsedMilliseconds() - time >= 3000
Debug"____"
ProcedureReturn result
Else
Debug "NO CONNECTION"
EndIf
EndProcedure
InitNetwork()
test.post_structure
setHttpPostUrl(@test,"http://www.thyphoon.com/test.php")
setProxy(@test,"spxy.bpi.fr",3128)
addHttpPostVariable(@test,"variable","unevaleur")
;addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileA")
;addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileB")
MessageRequester("result",sendHttpDatas(@test),#PB_MessageRequester_Ok)
Code : Tout sélectionner
Structure post_variable
name.s ;Variable Name
value.s ;Variable Value
EndStructure
Structure post_file
actionName.s ; this is important!! this action must be the same as <form ... name="file">
path.s ; path+filename about file to send
mime.s ; mime-type
EndStructure
Structure proxy_structure
server.s ;
port.l ;
login.s ;
password.s ;
EndStructure
Structure post_structure
url.s ;url to send POST
proxy.proxy_structure
List variable.post_variable() ;variables
List file.post_file() ;files
EndStructure
;Init Url to send Post Data
Procedure setProxy(*posts.post_structure,server.s,port.l,login.s="",password.s="")
*posts\proxy\server=server
*posts\proxy\port=port
*posts\proxy\login=login
*posts\proxy\password=password
EndProcedure
Procedure setHttpPostUrl(*posts.post_structure,url.s)
*posts\url=url
EndProcedure
;add a variable to the Post Data List
Procedure addHttpPostVariable(*posts.post_structure,name.s,value.s)
AddElement(*posts\variable())
*posts\variable()\name=name
*posts\variable()\value=value
EndProcedure
;add a file to the Post Data List
Procedure addHttpPostfiles(*posts.post_structure,file.s,actionName.s="file")
If FileSize(file)>0
AddElement(*posts\file())
*posts\file()\path=file
*posts\file()\actionName=actionName
*posts\file()\mime="text/plain";GetMimeType(GetExtensionPart(file))
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Macro addtoMemorie(__txt)
msize.l=MemorySize(*buffer)
Debug "size:"+Str(size)
If msize=1:msize=0:EndIf
newmsize=msize+StringByteLength(__txt,#PB_Ascii)
*buffer=ReAllocateMemory(*buffer,newmsize)
Debug Str(newmsize)+":"+__txt
PokeS(*buffer+msize,__txt,StringByteLength(__txt,#PB_Ascii),#PB_Ascii)
EndMacro
Procedure.s sendHttpDatas(*posts.post_structure)
Protected url.s,path.s,port.l,connection.i,f.i,POST.s,BOUNDARY.s
url = GetURLPart(*posts\url, #PB_URL_Site); the main domain
path =GetURLPart(*posts\url,#PB_URL_Path); the path
port= Val(GetURLPart(*posts\url, #PB_URL_Port))
If port=0:port=80:EndIf
POST=""
BOUNDARY = "-----------------------------306262331012750" ;http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
Lenght.l=0 ;
Debug "Connection opened ..."
*buffer=AllocateMemory(1)
;-Send Variable
;http://www.purebasic.fr/english/viewtopic.php?f=13&t=13759
If ListSize(*posts\variable())
size.l=MemorySize(*buffer)
ForEach *posts\variable()
addtoMemorie("Content-Disposition: form-Data; name="+Chr(34)+*posts\variable()\name+Chr(34)+#CRLF$)
addtoMemorie(#CRLF$)
addtoMemorie(*posts\variable()\value+#CRLF$)
addtoMemorie("--"+BOUNDARY+#CRLF$)
Next
Lenght+MemorySize(*buffer)-size
EndIf
;-Send File
ForEach *posts\file()
f = OpenFile(#PB_Any, *posts\file()\path)
If f
Debug "send file:"+GetFilePart(*posts\file()\path)
fileHeader.s="Content-Disposition: form-data; name="+Chr(34)+*posts\file()\actionName+Chr(34) +"; filename="+Chr(34)+*posts\file()\path+Chr(34)+#CRLF$
fileHeader+"Content-Type: application/octet-stream" + #CRLF$ + #CRLF$
Lenght+(Lof(f)+2*Len(BOUNDARY)+Len(fileHeader)+10)
addtoMemorie(#CRLF$)
addtoMemorie("--"+BOUNDARY+#CRLF$+fileHeader)
;addfile content
size.l=MemorySize(*buffer)
If size=1:size=0:EndIf
*buffer=ReAllocateMemory(*buffer,size+Lof(f))
ReadData(f, *buffer+size, Lof(f))
addtoMemorie(#CRLF$ + "--" + BOUNDARY + "--")
CloseFile(f)
Else
MessageRequester("Error","File No Found :"+#CRLF$+*posts\file()\path,#PB_MessageRequester_Ok)
EndIf
Next
Debug PeekS(*buffer,MemorySize(*buffer),#PB_Ascii)
;-TEST SI PROXY
;http://www.purebasic.fr/english/viewtopic.php?f=12&t=10327
If *posts\proxy\server<>""
connection = OpenNetworkConnection(*posts\proxy\server, *posts\proxy\port)
POST + "POST "+*posts\url+" HTTP/1.0"+ #CRLF$
If *posts\proxy\login<>""
conc$=*posts\proxy\login+":"+*posts\proxy\password
*OutputBuffer = AllocateMemory(Len(conc$)*4)
Base64Encoder(@conc$,Len(conc$),*OutputBuffer,Len(conc$)*4)
enc$=PeekS(*OutputBuffer)
FreeMemory(*OutputBuffer)
POST +"Proxy-Authorization: Basic "+enc$+#CRLF$
EndIf
Else
connection = OpenNetworkConnection(url, port)
POST + "POST "+"/"+path+" HTTP/1.0"+ #CRLF$
EndIf
POST + "Host: "+url + #CRLF$
POST + "Content-Type: multipart/form-data; boundary="+BOUNDARY + #CRLF$
;POST + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7" + #CRLF$
;si il y a un acces proteger avec htaccess
;POST + "Authorization: Basic "+ OutputBuffer + #CRLF$
POST + "Content-Length: "+Str(Lenght) + #CRLF$
POST + "Connection: keep-alive" + #CRLF$
If connection
CreateFile(0,"Debug.txt")
SendNetworkData(connection, @POST, Len(POST)):
WriteString(0,POST)
SendNetworkData(connection, *buffer, MemorySize(*buffer)):
Debug PeekS(*buffer,MemorySize(*buffer),#PB_Ascii)
WriteData(0,*buffer, MemorySize(*buffer))
CloseFile(0)
RunProgram("Debug.txt")
FreeMemory(*buffer)
string.s = Space(2048)
result.s=""
time = ElapsedMilliseconds()
Repeat
If NetworkClientEvent(Connection) = #PB_NetworkEvent_Data
readed = ReceiveNetworkData(connection, @string, 2048)
result + Left(string, readed)
time = ElapsedMilliseconds()
EndIf
Delay(100)
Until ElapsedMilliseconds() - time >= 3000
Debug"____"
ProcedureReturn result
Else
Debug "NO CONNECTION"
EndIf
EndProcedure
InitNetwork()
test.post_structure
setHttpPostUrl(@test,"http://www.thyphoon.com/test.php")
;setProxy(@test,"test.bpi.fr",80)
addHttpPostVariable(@test,"variable","unevaleur")
addHttpPostVariable(@test,"test","reponse")
addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileA")
addHttpPostfiles(@test,OpenFileRequester("Please choose file to load", "", "*.*", 0),"fileB")
MessageRequester("result",sendHttpDatas(@test),#PB_MessageRequester_Ok)