Page 1 of 2

Send POST data to web server

Posted: Thu Aug 06, 2009 4:00 pm
by Phantomas
Hello, how to send POST data to web server?
It is necessary to fill two fields (login, password) and push the button of "login", and after logging show HTML document.

Can you show example? Thanks...

Posted: Fri Aug 07, 2009 5:12 am
by Phantomas
No ideas? Please...

Posted: Fri Aug 07, 2009 5:42 am
by Tomi
http://www.purebasic.fr/english/viewtop ... highlight=
and search about FTP things
or see in "PureBasic CodeArchiv v4.0 Beta"
http://www.purebasic.fr/english/viewtop ... codearchiv

Posted: Fri Aug 07, 2009 5:28 pm
by Slyvnr
This is a program I wrote about a year ago to process data from a web-API program. Not sure if the userid and password(key) still work but you can get the idea.

Code: Select all

result=InitNetwork()
Debug result
If result
  ; these are the fields being posted for the system to call back the data
  ; not sure if these codes are still valid for getting data
  apiUSERID.s="2196844"
  apiUSERKEY.s="Xm1GcG9eVCbNoZXZApZe1Vs9FZf2qHrNpcRYOr34X4i7uEcyVc9htLeWUqvyZyem"
  
  ; web page to post to
  hostpgm$="http://api.eve-online.com/account/Characters.xml.aspx?"
  hostdata$="userID="+apiUSERID+"&apiKey="+apiUSERKEY
  lenhostdata=Len(hostdata$)
  lenhd$=Str(lenhostdata)

  hostconnectionstring$=hostpgm$+hostdata$
  Debug hostconnectionstring$
  ConnectionID = OpenNetworkConnection("api.eve-online.com", 80)
  Debug ConnectionID
  If ConnectionID
  
  ; proper format
  com$="POST "+hostconnectionstring$+" HTTP/1.1"+Chr(13)+Chr(10)
  com$=com$+"Accept: */*"+Chr(13)+Chr(10)
  com$=com$+"Accept: text/html"+Chr(13)+Chr(10)
  com$=com$+"Host: "+"api.eve-online.com"+Chr(13)+Chr(10)
  com$=com$+"User-Agent: Skill Parser"+Chr(13)+Chr(10)
  com$=com$+"Content-Length: " + lenhd$ + Chr(13) + Chr(10)
  com$=com$+Chr(13)+Chr(10)
  
  ; open the network
  Res = SendNetworkData(ConnectionID,@com$,Len(com$)) 
  ; POST the request
  Res = SendNetworkData(ConnectionID,@hostconnectionstring$,Len(hostconnectionstring$)) 

      Repeat
      Delay(100)
      Result = NetworkClientEvent(ConnectionID)
      Debug Result
      
      Select Result
 
      Case 2
        Content$ = Space(14500)
        ReceiveNetworkData(ConnectionID,@Content$,14500)
        Debug Content$
        Ok = 1
        CloseNetworkConnection(ConnectionID)
     EndSelect
 
    Until Ok = 1
  EndIf 
 Else
  MessageRequester("Error","NO TCP/IP Stack to call",#PB_MessageRequester_Ok)
EndIf

;write the response web-page to a txt file for processing by other program.
filnum=OpenFile(#PB_Any,"c:\test.txt")
If filnum
  WriteString(filnum,Content$)
EndIf
CloseFile(filnum)
Hope this helps

Slyvnr

Posted: Fri Aug 07, 2009 6:36 pm
by Kukulkan
Hi Slyvnr,

Does your example work on SSL secure HTTPS:// sites, too? If not, the only way is to use windows API...

Kukulkan

Posted: Fri Aug 07, 2009 6:59 pm
by Slyvnr
1) when I refer to API I am referring to a specific non-Windows API that this particular company provides for accessing data from their game servers outside of the game.

2) I have no idea on secure servers (https:) as I have never tried it. As you can see from the example it is a simple http: POST. It will work depending on how the "secure" portion of https: works and whether or not you can call some .dll or as you say Windows-API that provides the encryption mechanics.

Slyvnr

Posted: Tue Aug 18, 2009 3:20 pm
by Phantomas
Strange problem...

I refer GET request for server with browser imitation (Firefox):

Code: Select all

InitNetwork()

Connect = OpenNetworkConnection("88.191.63.41", 80)

If Connect
  two$ = "GET /index.php HTTP/1.1" + Chr(13) + Chr(10)
  two$ = two$ + "Host: www.purebasic.com" + Chr(13) + Chr(10)
  two$ = two$ + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1) Gecko/20090624 Firefox/3.5" + Chr(13) + Chr(10)
  two$ = two$ + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + Chr(13) + Chr(10)
  two$ = two$ + "Accept-Language: ru,en-us;q=0.7,en;q=0.3" + Chr(13) + Chr(10)
  two$ = two$ + "Accept-Encoding: gzip,deflate" + Chr(13) + Chr(10)
  two$ = two$ + "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7" + Chr(13) + Chr(10)
  two$ = two$ + "Keep-Alive: 300" + Chr(13) + Chr(10)
  two$ = two$ + "Connection: keep-alive" + Chr(13) + Chr(10)
  two$ = two$ + Chr(13) + Chr(10)
  
  SendNetworkData(Connect, @two$, Len(two$))
  
  two$ = ""
  Debug ReceiveNetworkData(Connect, @two$, 5000)
  
  If CreateFile(0, "C:/2.txt")
    WriteString(0, two$)
  EndIf
Else
  End
EndIf
After execution of this code: server will answer me page of the site (HTML code) and this code write to "C:/2.txt" file... I open this file and see size (symbols) of file: 2 920.
Why 2 920 rather 5000 — ReceiveNetworkData(Connect, @two$, 5000)???

Image

Thanks!

Posted: Tue Aug 18, 2009 4:16 pm
by gnozal
Phantomas wrote:After execution of this code: server will answer me page of the site (HTML code) and this code write to "C:/2.txt" file... I open this file and see size (symbols) of file: 2 920.
Why 2 920 rather 5000 — ReceiveNetworkData(Connect, @two$, 5000)???
Well, what does 'Debug ReceiveNetworkData(Connect, @two$, 5000)' display in the debugger ?
Purebasic.chm wrote:Result = ReceiveNetworkData(Connection, *DataBuffer, DataBufferLength)
...
Receives a raw data from the specified client. This function can be used by both client and server applications.
...
The 'Result' returns the number of bytes effectively read. If 'Result' is equal to DataBufferLength, then more data is available to be read. If an error occured on the connection (link broken, connection close by the server etc.), 'Result' will be -1.
Also, your data buffer should be at least 5000 bytes long ... You wrote 'two$ = ""' !

Posted: Tue Aug 18, 2009 4:37 pm
by Phantomas
gnozal wrote:
Phantomas wrote:After execution of this code: server will answer me page of the site (HTML code) and this code write to "C:/2.txt" file... I open this file and see size (symbols) of file: 2 920.
Why 2 920 rather 5000 — ReceiveNetworkData(Connect, @two$, 5000)???
Well, what does 'Debug ReceiveNetworkData(Connect, @two$, 5000)' display in the debugger ?
2920.

Maybe problem is solved: server sends answer on parts, necessary to loop RecieveNetworkData. Thanks & sorry.

Loop ReceiveNetworkData

Posted: Wed Aug 19, 2009 1:34 pm
by Slyvnr
Yes you need to loop ReceiveNetworkData until no more data.

I did it in my sample code in this section

Code: Select all

Repeat
      Delay(100)
      Result = NetworkClientEvent(ConnectionID)
      Debug Result
     
      Select Result
 
      Case 2
        Content$ = Space(14500)
        ReceiveNetworkData(ConnectionID,@Content$,14500)
        Debug Content$
        Ok = 1
        CloseNetworkConnection(ConnectionID)
     EndSelect
 
Until Ok = 1 
This basically waits for the network buffer to fill up so to speak. Then checks to see if it is full and if it is gets the data. I used 14,500 byte buffer size because that is how big the response should be.

Hope this helps.

Slyvnr

Re: Send POST data to web server

Posted: Mon May 02, 2011 9:50 am
by alokdube
you could invoke curl for windows via the cli

Re: Send POST data to web server

Posted: Mon May 02, 2011 11:41 am
by em_uk
Why would you wan to use an external program when you can code the desired affect yourself?

Re: Send POST data to web server

Posted: Mon May 02, 2011 12:26 pm
by alokdube
cookies. they are a big problem till you want to hand code them, the cookie-jar is a good thingie

Re: Send POST data to web server

Posted: Mon May 02, 2011 12:37 pm
by Nituvious
You can try to query the URL using GetHTTPHeader().

Re: Send POST data to web server

Posted: Mon May 02, 2011 1:01 pm
by alokdube
try it with any public email server :)