HTTP POST and GET method

Just starting out? Need help? Post your questions and find answers here.
shawn
New User
New User
Posts: 4
Joined: Sun Sep 26, 2004 9:08 pm

HTTP POST and GET method

Post by shawn »

Hi there!

I searched this forum a lot, but can't get a solution for my problem:

I need to do a HTTP Request, both GET and POST methods. Is there a finished solution for this somewhere?

I want to load a page from a webserver as a string, and want to put back data which requires doing a POST.

Thank you!
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

For Get: Search in the Ticks 'n' Tips - section for HTTP_
shawn
New User
New User
Posts: 4
Joined: Sun Sep 26, 2004 9:08 pm

POST solution

Post by shawn »

Hi,

thanks! I found several GET method solutions in the forum here, e.g. yours. But I need the POST, too.

The PHP Script on the server side reads the the data from the $_POST variable, so the $_GET is unset and it will not work. I can't modify the server script because it's not mine. I'll need a PB solution that supports HTTP POST.

I found this working php script solution that is based on socket connections. I hope to get it to work in PB, if no other PB solution exists.

Code: Select all

<?php

$timeout = 100;  		// Max time for stablish the conection
$size    = 0;  		// Bytes will be read (and display). 0 for read all
$server  = 'x.x.x.x';	            	// IP address
$host    = 'test.com';	            	// Domain name
$target  = '/post_or_get.php';     	// Specific program
$referer = 'http://any.referer/'; 	// Referer
$port    = 80;

// Setup an array of fields to get with then create the get string
$gets="";
//$gets = array ( 'get_field_1' => 'somevalue',
//                'get_field_2' => 'somevalue' );

// Setup an array of fields to post with then create the post string
$posts = array ( 'post_field_1' => 'somevalue',
                 'post_field_2' => 'somevalue' );

// That's all. Now the program proccess $repeat times

$method = "GET";
if ( is_array( $gets ) ) {
   $getValues = '?';
   foreach( $gets AS $name => $value ){
       $getValues .= urlencode( $name ) . "=" . urlencode( $value ) . '&';
   }
   $getValues = substr( $getValues, 0, -1 );
} else {
   $getValues = '';
}

if ( is_array( $posts ) ) {
   $postValues="";
   foreach( $posts AS $name => $value ){
       $postValues .= urlencode( $name ) . "=" . urlencode( $value ) . '&';
   }
   $postValues = substr( $postValues, 0, -1 );
   $method = "POST";
} else {
   $postValues = '';
}

$request  = "$method $target$getValues HTTP/1.1\r\n";
$request .= "Host: $host\r\n";
$request .= 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) ';
$request .= "Gecko/20021204\r\n";
$request .= 'Accept: text/xml,application/xml,application/xhtml+xml,';
$request .= 'text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,';
$request .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$request .= "Accept-Language: en-us, en;q=0.50\r\n";
$request .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$request .= "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66\r\n";
$request .= "Keep-Alive: 300\r\n";
$request .= "Connection: keep-alive\r\n";
$request .= "Referer: $referer\r\n";
$request .= "Cache-Control: max-age=0\r\n";

if ( $method == "POST" ) {
   $lenght = strlen( $postValues );
   $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
   $request .= "Content-Length: $lenght\r\n";
   $request .= "\r\n";
   $request .= $postValues;
}

for ( $i = 0; $i < $repeat; $i++ ) {
   $socket  = fsockopen( $server, $port, $errno, $errstr, $timeout );
   fputs( $socket, $request );
   if ( $size > 0 ) {
       $ret = fgets( $socket, $size );
   } else {
       $ret = '';
       while ( !feof( $socket ) ) {
           $ret .= fgets( $socket, 4096 );
       }
   }
   fclose( $socket );
}

?>
This one is easy to understand. Does PB internally supports Socket Connections or could this only done by Win API?
I remember a post here in the forum where the EOF was tricky. Is this a unsolved problem in PB?
Some server use GZIP compression for content. Is something like GZIPdeflate() supported in PB?
shawn
New User
New User
Posts: 4
Joined: Sun Sep 26, 2004 9:08 pm

got it to work

Post by shawn »

Hi,

got it to work. This is quick and dirty, but works...

Code: Select all


server$   = "x.x.x.x"              ; IP address 
host$     = "www.yourdomain.com"              ; Domain name 
target$   = "/test.php" ; Specific program 
referer$  = "http://www.google.de/"       ; Referer 
port.l    = 80                            ; Port 

post$     = "test1=bla&test2=foo"
length = Len(post$)
lenstr$ = Str(length)

request$  = "POST " + target$ + " HTTP/1.1" + Chr(13) + Chr(10)
request$  + "Host: " + host$ + Chr(13) + Chr(10) 
request$  + "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204" + Chr(13) + Chr(10)
request$  + "Accept: text/xml,application/xml,application/xhtml+xml," 
request$  + "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png," 
request$  + "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1" + Chr(13) + Chr(10) 
request$  + "Accept-Language: en-us, en;q=0.50" + Chr(13) + Chr(10) 
request$  + "Accept-Encoding: gzip, deflate, compress;q=0.9" + Chr(13) + Chr(10) 
request$  + "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66" + Chr(13) + Chr(10) 
request$  + "Keep-Alive: 300" + Chr(13) + Chr(10) 
request$  + "Connection: keep-alive" + Chr(13) + Chr(10) 
request$  + "Referer: " + referer$ + Chr(13) + Chr(10) 
request$  + "Cache-Control: max-age=0" + Chr(13) + Chr(10) 
request$  + "Content-Type: application/x-www-form-urlencoded" + Chr(13) + Chr(10)
request$  + "Content-Length: " + lenstr$ + Chr(13) + Chr(10) 
request$  + Chr(13) + Chr(10) 
request$  + post$ 

InitNetwork()
ConnectionID = OpenNetworkConnection(host$, port.l) 

SendNetworkString(ConnectionID, request$) 

While NetworkClientEvent(ConnectionID) <> 2 
  Delay(1) 
Wend 
*Buffer = AllocateMemory(50000) 
ReceiveNetworkData(ConnectionID, *Buffer, 50000) 
Text.s = PeekS(*Buffer) 
FreeMemory(*Buffer) 

CloseNetworkConnection(ConnectionID) 

Debug Text.s

End
It's easy. I'm wondering, why there was no solution in this forum.

I found no URLENCODE. Is there one in PB?
Is it possible to deflate GZIP compressed content in PB?
Golfy
User
User
Posts: 97
Joined: Wed Mar 21, 2012 6:10 pm

Re: HTTP POST and GET method

Post by Golfy »

I've searched for POST code. I then have tried by myself (thanks to Wireshark and http://www.hashemian.com/tools/form-pos ... hp/test123 too).
Now I'm proud to give you a simple procedure to post datas to an HTTP server.

Code: Select all

Procedure.s PostPHP(Link$, PostData$, Useragent$="Purebasic API", port.i=80, Timeout = 1000)
  ; Link$ is "http://server.domain/folder/folder/file.extension"
  ; PostData$ is all datas to post (example "Id=5&Name=Toto&Sex=M")
  BufSize.i = 4096
  Stop$=Chr(13)+Chr(10)   ; /r /n
  LenString$=Str(Len(PostData$))
  RelativeLink$=Right(Link$,Len(Link$)-FindString(Link$,"/",8)+1)
  PostString$ = "POST "+RelativeLink$+" HTTP/1.1"+stop$
  n = CountString(Link$,"/")
  Server$=StringField(Link$,3,"/")
  If UCase(StringField(Link$,1,"/"))="HTTP:"
    Debug RelativeLink$
    com$ = PostString$ + "Host: "+Server$+Stop$ + "User-Agent: "+Useragent$+Stop$
    com$ + "Accept: text/html,*/*"+Stop$
    com$ + "Content-Type: application/x-www-form-urlencoded"+Stop$
    com$ + "Content-Length: "+LenString$+Stop$+Stop$
    com$ + PostData$
    ConnectionID = OpenNetworkConnection(Server$, port.i,#PB_Network_TCP)
    If ConnectionID
      Res = SendNetworkData(ConnectionID,@com$,Len(com$))
      Debug com$
      Debug "-------"
      Delay(30)  ; let's time to receive the message
      rep$ = ""
      r$ = Space(4096)
      d = ElapsedMilliseconds()
      
      ; Read network up to Timeout time and return the answer.
      Repeat
        Result = NetworkClientEvent(ConnectionID)
        If result
          receivLen = ReceiveNetworkData(ConnectionID,@r$,BufSize)
          If receivLen < 0
            ProcedureReturn rep$
          EndIf
          If receivLen > 0 And receivLen <= BufSize
            d = ElapsedMilliseconds()
            rep$ + Left(r$,receivLen)
            Debug "["+Left(r$,receivLen)+"]"
          EndIf
        EndIf
        Tm = ElapsedMilliseconds()-d
      Until Tm > Timeout
      CloseNetworkConnection(ConnectionID)
      ProcedureReturn rep$
    EndIf
  EndIf
EndProcedure

result=InitNetwork()
If result
  Debug PostPHP("http://posttestserver.com/post.php","Login=Golfy"+"&"+"userID=01234"+"&"+"Comment=demo")
  Debug PostPHP("http://www.hashemian.com/tools/form-post-tester.php/test123","Login=Golfy"+"&"+"userID=01234"+"&"+"Comment=demo")
EndIf 
End
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: HTTP POST and GET method

Post by ricardo »

Golfy wrote:I've searched for POST code. I then have tried by myself (thanks to Wireshark and http://www.hashemian.com/tools/form-pos ... hp/test123 too).
Now I'm proud to give you a simple procedure to post datas to an HTTP server.

Code: Select all

Procedure.s PostPHP(Link$, PostData$, Useragent$="Purebasic API", port.i=80, Timeout = 1000)
  ; Link$ is "http://server.domain/folder/folder/file.extension"
  ; PostData$ is all datas to post (example "Id=5&Name=Toto&Sex=M")
  BufSize.i = 4096
  Stop$=Chr(13)+Chr(10)   ; /r /n
  LenString$=Str(Len(PostData$))
  RelativeLink$=Right(Link$,Len(Link$)-FindString(Link$,"/",8)+1)
  PostString$ = "POST "+RelativeLink$+" HTTP/1.1"+stop$
  n = CountString(Link$,"/")
  Server$=StringField(Link$,3,"/")
  If UCase(StringField(Link$,1,"/"))="HTTP:"
    Debug RelativeLink$
    com$ = PostString$ + "Host: "+Server$+Stop$ + "User-Agent: "+Useragent$+Stop$
    com$ + "Accept: text/html,*/*"+Stop$
    com$ + "Content-Type: application/x-www-form-urlencoded"+Stop$
    com$ + "Content-Length: "+LenString$+Stop$+Stop$
    com$ + PostData$
    ConnectionID = OpenNetworkConnection(Server$, port.i,#PB_Network_TCP)
    If ConnectionID
      Res = SendNetworkData(ConnectionID,@com$,Len(com$))
      Debug com$
      Debug "-------"
      Delay(30)  ; let's time to receive the message
      rep$ = ""
      r$ = Space(4096)
      d = ElapsedMilliseconds()
      
      ; Read network up to Timeout time and return the answer.
      Repeat
        Result = NetworkClientEvent(ConnectionID)
        If result
          receivLen = ReceiveNetworkData(ConnectionID,@r$,BufSize)
          If receivLen < 0
            ProcedureReturn rep$
          EndIf
          If receivLen > 0 And receivLen <= BufSize
            d = ElapsedMilliseconds()
            rep$ + Left(r$,receivLen)
            Debug "["+Left(r$,receivLen)+"]"
          EndIf
        EndIf
        Tm = ElapsedMilliseconds()-d
      Until Tm > Timeout
      CloseNetworkConnection(ConnectionID)
      ProcedureReturn rep$
    EndIf
  EndIf
EndProcedure

result=InitNetwork()
If result
  Debug PostPHP("http://posttestserver.com/post.php","Login=Golfy"+"&"+"userID=01234"+"&"+"Comment=demo")
  Debug PostPHP("http://www.hashemian.com/tools/form-post-tester.php/test123","Login=Golfy"+"&"+"userID=01234"+"&"+"Comment=demo")
EndIf 
End
How to keep a session using this method?
ARGENTINA WORLD CHAMPION
Post Reply