Network - How to do HTTP request and response?

Just starting out? Need help? Post your questions and find answers here.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Network - How to do HTTP request and response?

Post by va!n »

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
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Network - How to do HTTP request and response?

Post by Shield »

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. :)
Image
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
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Network - How to do HTTP request and response?

Post by infratec »

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.

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$)
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

Code: Select all

Content-Type: application/xml; charset=utf-8" + #CRLF$

Bernd
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Network - How to do HTTP request and response?

Post by Shield »

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.
Image
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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Network - How to do HTTP request and response?

Post by rsts »

Some info here may help (Windows only)

http://www.purebasic.fr/english/viewtop ... getfromweb
ynkrc
User
User
Posts: 49
Joined: Sat Sep 03, 2011 5:28 am

Re: Network - How to do HTTP request and response?

Post by ynkrc »

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 
TheoG
New User
New User
Posts: 8
Joined: Fri Jun 16, 2023 7:04 pm

Re: Network - How to do HTTP request and response?

Post by TheoG »

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?
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Network - How to do HTTP request and response?

Post by infratec »

This code is totally outdated.
It is better to rewrite it from scratch with the newer available HTTPRequest() stuff.
TheoG
New User
New User
Posts: 8
Joined: Fri Jun 16, 2023 7:04 pm

Re: Network - How to do HTTP request and response?

Post by TheoG »

-deleted
Last edited by TheoG on Sat Jun 17, 2023 10:52 am, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 16687
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Network - How to do HTTP request and response?

Post by Fred »

Please stop posting the same stuff all over the forum, thank you.
TheoG
New User
New User
Posts: 8
Joined: Fri Jun 16, 2023 7:04 pm

Re: Network - How to do HTTP request and response?

Post by TheoG »

You are welcome.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Network - How to do HTTP request and response?

Post by mk-soft »

TheoG wrote: Sat Jun 17, 2023 10:58 am You are welcome.
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
Post Reply