Page 1 of 2

Simulating HTTP(S) POST

Posted: Wed Nov 12, 2003 12:15 am
by Karbon
Finally poked around enough..

This one does SSL, all you need to do for standard HTTP is change the INTERNET_FLAG_SECURE to 0 and the port to 80.

Code: Select all

  ;
  ; All stuff for the WinInet lib.
  ;
  #INTERNET_OPEN_TYPE_DIRECT = 1
  #HTTP_ADDREQ_FLAG_ADD = $20000000
  #HTTP_ADDREQ_FLAG_REPLACE = $80000000
  #INTERNET_FLAG_SECURE = $800000
  
  ;
  ; Type of connection (could be FTP Gopher etc). HTTPS is done as HTTP too.
  ;
  #INTERNET_SERVICE_HTTP = 3
  
  ;
  ; HTTP port is 80, HTTPS (SSL) port is 443. 
  ;
  #INTERNET_DEFAULT_HTTP_PORT = 443  

Procedure do_post()
 
  ;
  ; Do NOT include http:// or any other protocol indicator here
  ;
  host.s = "secure.example.com"
  
  ;
  ; Everything after the hostname of the server
  ;
  get_url.s = "/pb_test.php"
  
  ;
  ; Holds the result from the CGI/page 
  ;
  result.s = ""
  
  ;
  ; All from the wininet DLL
  ;
  ; Be sure your Internet Explorer is up to date!
  ;
  open_handle = InternetOpen_("User Agent Info Goes Here",#INTERNET_OPEN_TYPE_DIRECT,"","",0)
  
  connect_handle = InternetConnect_(open_handle,host,#INTERNET_DEFAULT_HTTP_PORT,"","",#INTERNET_SERVICE_HTTP,0,0)
  
  request_handle = HttpOpenRequest_(connect_handle,"POST",get_url,"","",0,#INTERNET_FLAG_SECURE,0)
  
  headers.s = "Content-Type: application/x-www-form-urlencoded" +Chr(13)+Chr(10)  
  
  HttpAddRequestHeaders_(request_handle,headers,Len(headers), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD)
  
  post_data.s = "testval=dootdedootdoot"
  
  post_data_len = Len(post_data)
  
  send_handle = HttpSendRequest_(request_handle,"",0,post_data,post_data_len)
  
  buffer.s = Space(1024) 
  
  bytes_read.l
  
  total_read.l 
  
  total_read = 0
  
  ;
  ; Read until we can't read anymore.. 
  ; The string "result" will hold what ever the server pushed at us.
  ;
  Repeat
    
    InternetReadFile_(request_handle,@buffer,1024,@bytes_read)
    
    result + Left(buffer,bytes_read)
    
    buffer = Space(1024)
    
  Until bytes_read=0
  
EndProcedure

Posted: Wed Nov 12, 2003 8:46 am
by benny
Thanks for this nice commented code! :D
It will help me for sure in future !

Posted: Thu Jun 02, 2005 5:53 pm
by Blade
Thanks Karbon, you saved my life! :)

Posted: Thu Jun 02, 2005 9:43 pm
by zapman*
Very nice, thanks!

Posted: Thu Jun 02, 2005 11:42 pm
by NoahPhense
sweet!

- np

Posted: Fri Jun 03, 2005 2:39 pm
by Karbon
Wow, this one was old!

Posted: Fri Jun 03, 2005 9:26 pm
by dell_jockey
old code never dies, it just fades away... :)

Whatever, that snippet was very useful to me as well, thanks for sharing!

Posted: Fri Jun 03, 2005 10:54 pm
by NoahPhense
nothing good, dies... it only evolves...

- np

Posted: Wed Mar 01, 2006 2:42 pm
by Bonne_den_kule
How can I make this code to store the cookies ? (a login page I tested said that "the browser" had turned cookies off).

Edit:
Not the pb login page

Posted: Sun Mar 05, 2006 8:01 am
by lexvictory
This snippet even handles 'Location:' headers!
make it do a get request instead of a post, and point it to http://droopyslib.hollosite.com/ - it will get the new site! (http://gansta93.free.fr/droopyslib)!!!

Posted: Sun Mar 05, 2006 8:21 am
by lexvictory
Bonne_den_kule wrote:How can I make this code to store the cookies ? (the login page I tested said that "the browser" had turned cookies off).

log in to the purebasic forum:

Code: Select all

; 
  ; All stuff for the WinInet lib. 
  ; 
  #INTERNET_OPEN_TYPE_DIRECT = 1 
  #HTTP_ADDREQ_FLAG_ADD = $20000000 
  #HTTP_ADDREQ_FLAG_REPLACE = $80000000 
  #INTERNET_FLAG_SECURE = 0 
  
  ; 
  ; Type of connection (could be FTP Gopher etc). HTTPS is done as HTTP too. 
  ; 
  #INTERNET_SERVICE_HTTP = 3 
  
  ; 
  ; HTTP port is 80, HTTPS (SSL) port is 443. 
  ; 
  #INTERNET_DEFAULT_HTTP_PORT = 80  
  
  ;For httpqueryinfo.
  #HTTP_QUERY_COOKIE = 44

Procedure.s do_post(username.s, password.s) 
  
  ; 
  ; Do NOT include http:// or any other protocol indicator here 
  ; 
  host.s = "purebasic.fr" 
  
  ; 
  ; Everything after the hostname of the server 
  ; 
  get_url.s = "/english/login.php" 
  
  ; 
  ; Holds the result from the CGI/page 
  ; 
  result.s = "" 
  
  ; 
  ; All from the wininet DLL 
  ; 
  ; Be sure your Internet Explorer is up to date! 
  ; 
  open_handle = InternetOpen_("User Agent Info Goes Here",#INTERNET_OPEN_TYPE_DIRECT,"","",0) 
  
  connect_handle = InternetConnect_(open_handle,host,#INTERNET_DEFAULT_HTTP_PORT,"","",#INTERNET_SERVICE_HTTP,0,0) 
  
  request_handle = HttpOpenRequest_(connect_handle,"POST",get_url,"","",0,#INTERNET_FLAG_SECURE,0) 
  
  headers.s = "Content-Type: application/x-www-form-urlencoded" +Chr(13)+Chr(10)  
  
  HttpAddRequestHeaders_(request_handle,headers,Len(headers), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD) 
  
  ;change this when using a login form other than a phpbb one
  post_data.s = "username="+username+"&password="+password+"&login=Log%20in"; not sure why the submit button text is needed.... but it seemed to make it work for me :lol: 
  
  post_data_len = Len(post_data) 
  
  send_handle = HttpSendRequest_(request_handle,"",0,post_data,post_data_len) 
  
  buffer.s = Space(1024) 
  
  bytes_read.l 
  
  total_read.l 
  
  total_read = 0 
  
  ; 
  ; Read until we can't read anymore.. 
  ; The string "result" will hold what ever the server pushed at us. 
  ; 
  Repeat 
    
    InternetReadFile_(request_handle,@buffer,1024,@bytes_read) 
    
    result + Left(buffer,bytes_read) 
    
    buffer = Space(1024) 
    
  Until bytes_read=0 
  
  
  ;- uncomment the following when you want to get the cookie data - not sure if it works or not.....
  ;buffer.s = Space(#MAX_PATH)
  ;headernum = 0
  ;length = Len(buffer)
  ;HttpQueryInfo_(request_handle, #HTTP_QUERY_COOKIE, @buffer, @length, @headernum)
  ;Debug buffer
  
 ProcedureReturn result
  
EndProcedure 







SetClipboardText(do_post("YOUR USERNAME", "YOUR PASSWORD"))





MessageRequester("Done", "Paste text into your text editor!")

remember to put in your username and password :lol:

Posted: Tue May 30, 2006 3:47 pm
by Crystal Noir
this example is cool but how to display the result in the web browser gadget ? (and not copy the html source to clipboard ?)

Posted: Wed May 31, 2006 9:49 am
by lexvictory

Posted: Wed Oct 25, 2006 7:34 am
by Michael Vogel
Still having problems to handle post and get together, maybe you can give me a tip :?:

I'll try to check, if a telefon number for a certain provider (www.telering.at) is available automatically, and I've sniffered the connection, when I do this with the web browser by hand.

It seems for me, that only two packets (one POST and one GET) are necessary to getr a page with a message, that the number is available or not...


I tried to modify the example, but all I get is the starting page with my tried number (1234567) in the number field...

Code: Select all

EnableExplicit

; Define

	Global Handle
	Global SeitenHandle

	#BufferSize=8192

	#INTERNET_OPEN_TYPE_DIRECT = 1

	#HTTP_ADDREQ_FLAG_ADD = $20000000
	#HTTP_ADDREQ_FLAG_REPLACE = $80000000
	#INTERNET_FLAG_SECURE = $800000
	#INTERNET_FLAG_RELOAD = $80000000
	; Type of connection (could be FTP Gopher etc). HTTPS is done as HTTP too.
	#INTERNET_SERVICE_HTTP = 3
	; HTTP port is 80, HTTPS (SSL) port is 443.
	#INTERNET_DEFAULT_HTTP_PORT=80

	Global posttext.s="HTTP/1.1"+#CRLF$+"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*"+#CRLF$+"Referer: http://www.telering.at/Content.Node/mobil/wunschrufnummernrechner.php"+#CRLF$+"Accept-Language: de-at"+#CRLF$+"Content-Type: application/x-www-form-urlencoded"+#CRLF$+"Accept-Encoding: gzip, deflate"+#CRLF$+"User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; Maxthon)"+#CRLF$+"Host: www.telering.at"+#CRLF$+"Content-Length: 38"+#CRLF$+"Connection: Keep-Alive"+#CRLF$+"Cache-Control: no-cache"+#CRLF$+"Cookie: tid=KGEuDl2vG333JCZ"+#CRLF$+"Fnumber=0650&clicked=2&Snumber=1234567"
	Global gettext.s="HTTP/1.1"+#CRLF$+"Accept: */*"+#CRLF$+"Referer: http://www.telering.at/Content.Node/mobil/wunschrufnummernrechner.php"+#CRLF$+"Accept-Language: de-at"+#CRLF$+"Accept-Encoding: gzip, deflate"+#CRLF$+"If-Modified-Since: Wed, 19 Jul 2006 10:14:04 GMT"+#CRLF$+"If-None-Match: 21c3f7-60e-85dc5300"+#CRLF$+"User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; Maxthon)"+#CRLF$+"Host: www.telering.at"+#CRLF$+"Connection: Keep-Alive"+#CRLF$+"Cookie: tid=KGEuDl2vG333JCZ"
	;/Content.Node/z/css/jsmenupriv.css

	; Do NOT include http:// or any other protocol indicator here
	Global host.s = "www.telering.at"

	; Everything after the hostname of the server
	;get_url.s = "/Content.Node/mobil/wunschrufnummernrechner.php"

; EndDefine
Procedure.s GetContent(Handle)

	Protected Result.s=""
	Protected Buffer.s=Space(#BufferSize)
	Protected BytesRead.l
	Protected TotalRead.l=0

	Repeat
		InternetReadFile_(Handle,@Buffer,#BufferSize,@BytesRead)
		Result+Left(Buffer,BytesRead)
		Buffer=Space(#BufferSize)
	Until BytesRead=0

	ProcedureReturn Result

EndProcedure

Procedure.l InitConnection(Server.s)

	Protected OpenHandle
	Protected ConnectHandle
	Protected RequestHandle

	; All from the wininet DLL
	OpenHandle=InternetOpen_("4HIA Number Tracker",#INTERNET_OPEN_TYPE_DIRECT,"","",0)
	ConnectHandle=InternetConnect_(OpenHandle,Server,#INTERNET_DEFAULT_HTTP_PORT,"","",#INTERNET_SERVICE_HTTP,0,0)

	ProcedureReturn ConnectHandle
EndProcedure
Procedure.s DoRequest(Handle,Page.s,Type.s,Header.s,Info.s)
	Protected RequestHandle
	Protected SendHandle
	Protected InfoLen

	RequestHandle=HttpOpenRequest_(Handle,Type,Page,"","",0,#INTERNET_FLAG_SECURE,0)
	If Header="" : Header="Content-Type: application/x-www-form-urlencoded"+#CRLF$ : EndIf
	HttpAddRequestHeaders_(RequestHandle,Header,Len(Header),#HTTP_ADDREQ_FLAG_REPLACE|#HTTP_ADDREQ_FLAG_ADD)

	Debug RequestHandle
	InfoLen=Len(Info)
	SendHandle=HttpSendRequest_(RequestHandle,"",0,Info,InfoLen)

	ProcedureReturn GetContent(RequestHandle)
EndProcedure

Posted: Wed Oct 25, 2006 8:59 am
by lexvictory
y are u putting the headers in the posttext variable?