save jpg from a webserver

Windows specific questions.
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

hi, the size of a saved picture have to be ~ 180KB but this is: 65 290 bytes, the "disk space" is 65 536 bytes... hm.... it's interesting...
string variable can be 2GB, i can't understand, this is not the bug...
I think it is could be a size limit of a packet from server?? :]
thanks
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

dim as integer nBytes=96*1024
dim as lpImage = callocate(nBytes-1)
ret=NetRead(hSocket,lpImage,nBytes)
if ret<0 then
print "error code=" & ret
else
print "bytes readed=" & ret
end if

normaly you can get large chunks of bytes but i tested it with
an 70KB jpg and the web server sends back only the first
13068 bytes of the image
(same if you use the ret=NetReceiveString(hClient,msg))

seams to be you must send the right http request to get larger
or more chunks of data at all.

Joshy

take a look at:

ftp://ftp.isi.edu/in-notes/rfc2616.txt
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

pff... :( it's to difficult for me.. X/
i will try something...
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

You might find this a bit easier:

Code: Select all

#include "../chisock.bi"

using chi

dim as socket sock
sock.client( "subliminal-prodigy.xepher.net", port.HTTP )

sock.put_HTTP_request( "subliminal-prodigy.xepher.net/pics/glow.png" )

var the_data = sock.get_until( "" )

the_data = mid( the_data, instr( the_data, chr(13, 10, 13, 10) ) + 4 )

var ff = freefile
open "thing.png" for binary access write as #ff
	put #ff, , the_data[0], len(the_data)
close ff
You can get chisock here: http://www.freebasic.net/forum/viewtopic.php?t=8454
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

hey cha0s get my test picture :-)

Code: Select all

#include "net.bi"
#define crlf chr(13) & chr(10)

dim as FDSOCKET    hClient,hServer
dim as SOCKADDR_in ServerAddr,Iam
dim As HOSTENT ptr lpResolver
dim as string      msg
dim as integer     ret
dim as uinteger    ip
dim as string      servername="alice-dsl.net"

' create socket
hClient=NetSocket(AF_INET,SOCK_STREAM)
if hClient<0 then
  ? "error: create socket!"
  sleep:end 2
end if

' get ip from server
lpResolver=NetGetHostByName (servername)
if lpResolver=0 then
  ? "error: NetGetHostByName!"
  beep:sleep:end 3
else
  ip=*lpResolver->h_addr_ip_list[0] 'server ip
end if

with ServerAddr
  .sin_family = AF_INET    ' protocol
  .sin_port   = htons(80)  ' port 80   in network byte order
  .sin_addr   = ip         ' server ip in network byte order
end with

' connect to server
ret=NetConnect(hClient,@ServerAddr)
if ret<0 then
  ? "error: can't connect to server!"
  beep:sleep:end 4
end if

dim as string method,host,accept
method    = "GET /d.j.peters/test.jpg HTTP/1.1"  & crlf
host      = "Host: alice-dsl.net"  & crlf
accept    = "Accept-Encoding: gzip" & crlf
accept  & = "Accept: image/jpeg"   & crlf
msg       = method & host & accept & crlf

ret=NetSendString(hClient,msg)
if ret<=0 then
  ? "error: send string!"
  NetClose hClient
  beep:sleep:end 6
end if

dim as string Header,Image
print "get file test.jpg please wait ..."
print "---------------------------------"
ret=NetReceiveString(hClient,msg)
if ret<0 then
  ? "error: get file/site!"
  NetClose hClient
  beep:sleep
end if

while ret>0
  Image & = msg
  ret=NetReceiveString(hClient,msg)
wend
NetClose hClient


? len(msg)
ret =instr(image,crlf & crlf) 'search for header terminator
header=left(image,ret)
print header
print "---------------------------------"
ret+=3 ' jump over the header
Image =right(image,len(image)-ret)
'optional save the answer
open exepath + "\test.jpg" for output as #1
  print #1,Image
close #1
print "file:" & exepath + "\test.jpg saved"
sleep
end
Last edited by D.J.Peters on Feb 19, 2008 9:52, edited 1 time in total.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

hey Joshy, that looks like jailbait (if you don't recognize this word, good luck with the translation).

=====

It's simpler to use URLDownloadToFileA (should be searchable in this forum, should be in the examples but isn't?). I can't imagine a Windows machine without at least IE4 installed on it (necessary for URLDownloadToFileA) - yes, persons deliberately uninstall IE but they only do it ONCE.

And if one encounters a prob with dl-ed files not updating (caching prob) one can use DeleteUrlCacheEntry(szUrl) before the dl.

Another advantage with URLDownloadToFileA is that it'll dl from cgi output, a task that's otherwise difficult with roll-your-own code.
Post Reply