Network - How to do HTTP request and response?
Network - How to do HTTP request and response?
Since i have no knowledge in network / internet coding, it would be nice to get your help here. For a project where the uer can send SMS by a tool, the netprovider will give a special URL with user and password, to send SMS. This should work by sending a HTTP request something like:
HTTP-REQUEST: http://TheSpecialURL.com/sms%username&password< Here are the datas for the sms text, sender, receiver and so on in XML format>
Has somebody an idea, how to realize this and receiving the HTTP-RESPONSE code to check if the sms was send successfully (code 200) or not?
Due fact, the program is for extrem emergency case, the code must work 100% perfect without loosing data over network or getting wrong results.
Many thanks in advance
HTTP-REQUEST: http://TheSpecialURL.com/sms%username&password< Here are the datas for the sms text, sender, receiver and so on in XML format>
Has somebody an idea, how to realize this and receiving the HTTP-RESPONSE code to check if the sms was send successfully (code 200) or not?
Due fact, the program is for extrem emergency case, the code must work 100% perfect without loosing data over network or getting wrong results.
Many thanks in advance
va!n aka Thorsten
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Re: Network - How to do HTTP request and response?
What type of request is it? GET? POST? HEAD?
If you just need to call an URL and get the status code, GetHTTPHeader() might be enough.
However, since you mentioned XML data I suspect you need a POST request.
Since you are looking for a reliable way I wouldn't recommend a custom implementation,
so you might want to have a look at libcurl.
If you just need to call an URL and get the status code, GetHTTPHeader() might be enough.
However, since you mentioned XML data I suspect you need a POST request.
Since you are looking for a reliable way I wouldn't recommend a custom implementation,
so you might want to have a look at libcurl.
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Re: Network - How to do HTTP request and response?
Hi,
as already said: it depends if you need POST or GET
If you can type the request in a webbroser, than it is a GET request.
(like http://TheSpecialURL.com/sms?u=user&p=p ... 23&t=Hello)
If not, than it is a POST request.
In general you need always a header.
The header has 2 CRLFs at the end to mark the end.
A 'body' is needed if you have a POST request.
Than you have to receive all back comming data and check if there is a number between 200 and 299 inside the header,
which means Ok.
You have to build the body first, because you need the length for 'Content-Length:'
Than receive the answer as above.
If your data contains xml you have to change the content line
Bernd
as already said: it depends if you need POST or GET
If you can type the request in a webbroser, than it is a GET request.
(like http://TheSpecialURL.com/sms?u=user&p=p ... 23&t=Hello)
If not, than it is a POST request.
In general you need always a header.
The header has 2 CRLFs at the end to mark the end.
A 'body' is needed if you have a POST request.
Code: Select all
Header$ = "GET " + "/english/index.php" + " HTTP/1.1" + #CRLF$
Header$ + "Host: " + "www.purebasic.fr" + #CRLF$
Header$ + "Content-Type: application/x-www-form-urlencoded" + #CRLF$
Header$ + #CRLF$
; header is finished
;Debug Header$
SendNetworkString(ConnectionID, Header$)
Than you have to receive all back comming data and check if there is a number between 200 and 299 inside the header,
which means Ok.
Code: Select all
Post$ = "date=" + Date$ + "&"
Post$ + "clubId=" + Str(ClubID) + "&"
Post$ + "federation=RTTV"
;Build header
Header$ = "POST /cgi-bin/WebObjects/ClickSWTTV.woa/wa/ttrFilter HTTP/1.0" + #CRLF$
Header$ + "Host: rttv.click-tt.de" + #CRLF$
Header$ + "Accept: text/html" + #CRLF$
Header$ + "Content-Type: application/x-www-form-urlencoded" + #CRLF$
Header$ + "Content-Length: " + Str(Len(Post$)) + #CRLF$
Header$ + #CRLF$
; header is finished
;Debug Header$
SendNetworkString(ConnectionID, Header$ + Post$)
Than receive the answer as above.
If your data contains xml you have to change the content line
Code: Select all
Content-Type: application/xml; charset=utf-8" + #CRLF$
Bernd
Re: Network - How to do HTTP request and response?
If you need to receive the actual response (other than the plain response code from the header),
it gets much more difficult because you need to be able to handle chunked responses and possible compression.
(Compression is less of a problem but web servers just assume you will support chunked answers,
even if you didn't specify it in the request.)
Depending on your requirements I would really recommend using an existing framework.
it gets much more difficult because you need to be able to handle chunked responses and possible compression.
(Compression is less of a problem but web servers just assume you will support chunked answers,
even if you didn't specify it in the request.)
Depending on your requirements I would really recommend using an existing framework.
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Re: Network - How to do HTTP request and response?
this may be helpful to you!
Code: Select all
Procedure.s getbodys(URL.s, flag.l=0, timeOut.l=500, PacketSize.l=1024,UserAgent.s="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", *Callback=0)
Protected Size.l, Data_.s, File.s, Text.s="", Length.l=0, Line.s
Protected CurSize.l, oSize.l, t.l, ConnectionID.l
If Left(URL, 7) = "http://" : URL = Right(URL, Len(URL)-7) : EndIf
s = FindString(URL, "/", 1)
If s <> 0 : Host.s = Left(URL, s-1) : File.s = Right(URL, Len(URL)-s) : Else : Host = URL : EndIf
ConnectionID = OpenNetworkConnection(Host, 80)
If ConnectionID
Data_.s = "GET /"+File+" HTTP/1.0"+#CRLF$
Data_.s + "Host: "+Host+#CRLF$
Data_.s + "User-Agent: "+UserAgent+#CRLF$
Data_.s + "Connection: close"+#CRLF$+#CRLF$
SendNetworkString(ConnectionID, Data_)
While NetworkClientEvent(ConnectionID)<> #PB_NetworkEvent_Data : Delay(5) : Wend
Repeat
Line = ReceiveLine(ConnectionID)
Text=Text+line+#CRLF$
Select LCase(StringField(Line, 1, ":"))
Case "content-length"
Length = Val(Trim(StringField(Line, 2, ":")))
Case "content-type"
If flag=0
Select LCase(Trim(StringField(StringField(Line, 2, ":"),2,"=")))
Case "utf8","utf-8","big5"
flag=#PB_UTF8
Case "gb2312","gbk"
flag=#PB_Ascii
EndSelect
EndIf
EndSelect
Until Len(Trim(Line)) <= 1
*Buffer = AllocateMemory(PacketSize)
*Result = AllocateMemory(1)
If *Callback
CallFunctionFast(*Callback, Size,Length)
EndIf
If Length>0
Repeat
CurSize = ReceiveNetworkData(ConnectionID, *Buffer, PacketSize)
If CurSize>0
oSize = Size
size=Size + CurSize
*Result = ReAllocateMemory(*Result, Size,#PB_Memory_NoClear)
CopyMemory(*Buffer, *Result+oSize, CurSize)
If *Callback
CallFunctionFast(*Callback, Size,Length)
EndIf
EndIf
Until Size>=Length
Else
t = ElapsedMilliseconds():t2.l=t
Repeat
CurSize = ReceiveNetworkData(ConnectionID, *Buffer, PacketSize)
If CurSize>0
oSize = Size
size=Size + CurSize
*Result = ReAllocateMemory(*Result, Size,#PB_Memory_NoClear)
CopyMemory(*Buffer, *Result+oSize, CurSize)
Length=Size
If *Callback
CallFunctionFast(*Callback, Size,Length)
EndIf
EndIf
t2=ElapsedMilliseconds()
If NetworkClientEvent(ConnectionID)<>#PB_NetworkEvent_Disconnect
t=t2
EndIf
Until t2-t>=timeOut
EndIf
If NetworkClientEvent(ConnectionID)>0
CloseNetworkConnection(ConnectionID)
EndIf
Debug flag
If flag=0
temps.s=ReplaceString(LCase(PeekS(*Result,200,#PB_Ascii))," ","")
temps=StringField(temps,2,"charset=")
temps=Left(temps,FindString(temps,">")-1)
temps=RTrim(temps,"'")
temps=RTrim(temps,#DQUOTE$)
temps=ReplaceString(temps,"-","")
Select temps
Case "utf8"
flag=#PB_UTF8
Case "utf16"
flag=#PB_UTF16
Case "utf32"
flag=#PB_UTF32
Case "gbk","gb2312","gb2000"
flag=#PB_Ascii
Default
flag=#PB_Ascii
EndSelect
EndIf
text=text+PeekS(*Result,Size,flag)
ProcedureReturn text
EndIf
EndProcedure
Re: Network - How to do HTTP request and response?
I get an error "[20:17:17] [COMPILER] Line 2757: ReceiveLine() is not a function, array, list, map or macro."
Where did you define this function?
Where did you define this function?
Re: Network - How to do HTTP request and response?
This code is totally outdated.
It is better to rewrite it from scratch with the newer available HTTPRequest() stuff.
It is better to rewrite it from scratch with the newer available HTTPRequest() stuff.
Re: Network - How to do HTTP request and response?
-deleted
Last edited by TheoG on Sat Jun 17, 2023 10:52 am, edited 1 time in total.
Re: Network - How to do HTTP request and response?
Please stop posting the same stuff all over the forum, thank you.
Re: Network - How to do HTTP request and response?
You are welcome.
Re: Network - How to do HTTP request and response?
Hallo newby,
Fred is ower master, chef, guru, ...
What Fred writes is our law
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive