File: webget_demo.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (42 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function [pagedata,hdr]=webget_demo(url)
% WEBGET_DEMO - Demo webpage downloader, returns a webpage as a string
%
% Syntax:
%    webget_demo url_adress
%  or
%    str=webget_demo('url_adress')
%  
%  Version: 2002-02-01 for the tcpiptoolbox 2.x API
%
if nargin==0,    url='http://www.mathworks.com'; end
MAXPAGESIZE=1024*1024;
%Decode URL
if strncmp(url,'http:',5), url=url(6:end); end
if strncmp(url,'//',2),    url=url(3:end); end
[host,page]=strtok(url,'/');
[host,port]=strtok(host,':');
if length(port)>1, port=port(2:end); else port='80'; end
if length(page)==0, page='/'; end
%Connect to web server
con=pnet('tcpconnect',host,port);
if con==-1, error 'Bad url or server down.....'; end
disp(['Connected to: ' host]);
if 1,
  pnet(con,'setwritetimeout',1);
  pnet(con,'setreadtimeout',30);
  %Send request
  pnet(con,'printf','GET %s HTTP1.0\n\n',page);
  pnet(con,'read',MAXPAGESIZE,'view');   % Read data to local buffer with 30 sec timout.
  pnet(con,'setreadtimeout',0.1); %Change timeout
  hdrstr=''; hdr='';
  %Remove header
  while length(hdrstr) || length(hdr)==0,
    stat=pnet(con,'status');
    hdrstr=pnet(con,'readline');
    hdr=sprintf('%s%s\n',hdr,hdrstr);
  end
  %Return page
  pnet(con,'setreadtimeout',60);
  pagedata=pnet(con,'read',MAXPAGESIZE);
end
pnet(con,'close');