Send POST data to web server

Just starting out? Need help? Post your questions and find answers here.
Phantomas
User
User
Posts: 96
Joined: Wed Jul 01, 2009 12:59 pm

Send POST data to web server

Post 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...
Phantomas
User
User
Posts: 96
Joined: Wed Jul 01, 2009 12:59 pm

Post by Phantomas »

No ideas? Please...
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Post 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
Slyvnr
User
User
Posts: 58
Joined: Wed Jun 27, 2007 10:10 pm
Location: USA
Contact:

Post 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
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Post 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
Slyvnr
User
User
Posts: 58
Joined: Wed Jun 27, 2007 10:10 pm
Location: USA
Contact:

Post 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
Phantomas
User
User
Posts: 96
Joined: Wed Jul 01, 2009 12:59 pm

Post 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!
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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$ = ""' !
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Phantomas
User
User
Posts: 96
Joined: Wed Jul 01, 2009 12:59 pm

Post 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.
Slyvnr
User
User
Posts: 58
Joined: Wed Jun 27, 2007 10:10 pm
Location: USA
Contact:

Loop ReceiveNetworkData

Post 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
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

Re: Send POST data to web server

Post by alokdube »

you could invoke curl for windows via the cli
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: Send POST data to web server

Post by em_uk »

Why would you wan to use an external program when you can code the desired affect yourself?
----

R Tape loading error, 0:1
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

Re: Send POST data to web server

Post by alokdube »

cookies. they are a big problem till you want to hand code them, the cookie-jar is a good thingie
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Send POST data to web server

Post by Nituvious »

You can try to query the URL using GetHTTPHeader().
▓▓▓▓▓▒▒▒▒▒░░░░░
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

Re: Send POST data to web server

Post by alokdube »

try it with any public email server :)
Post Reply