Simulating HTTP(S) POST

Share your advanced PureBasic knowledge/code with the community.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Simulating HTTP(S) POST

Post 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
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

Thanks for this nice commented code! :D
It will help me for sure in future !
regards,
benny!
-
pe0ple ar3 str4nge!!!
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

Thanks Karbon, you saved my life! :)
zapman*
Enthusiast
Enthusiast
Posts: 115
Joined: Wed Jun 02, 2004 10:17 pm
Location: New Caledonia (South Pacific)
Contact:

Post by zapman* »

Very nice, thanks!
Don't try - DO it !
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

sweet!

- np
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Wow, this one was old!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

old code never dies, it just fades away... :)

Whatever, that snippet was very useful to me as well, thanks for sharing!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

nothing good, dies... it only evolves...

- np
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post 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
Last edited by Bonne_den_kule on Sun Mar 05, 2006 2:02 pm, edited 1 time in total.
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post 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)!!!
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post 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:
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
Crystal Noir
User
User
Posts: 38
Joined: Thu May 01, 2003 12:15 am

Post 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 ?)
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post by lexvictory »

Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post by lexvictory »

y are u putting the headers in the posttext variable?
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
Post Reply