复制了一部分。。- class netReq
- request
- method
- connective
- TimeOutSet
- function init()
- this.TimeOutSet = 10000;
- end
- function connect(method,url,addr)
- this.method = method;
- if this.method <> 'POST' and this.method <> 'GET' then
- if this.method = '' then
- print('Error: Null Method');
- return -1;
- end;
- print('Error: Unsupported Method');
- return -2;
- end;
- if url = '' then
- url = '/';
- end;
- this.request = this.method + ' ' + url + ' ' + 'HTTP/1.1\r\n';
- if not isnum(addr[1]) then
- print 'Error: Invalid port number, must be integer';
- return -10;
- end;
- io.timeout(this.TimeOutSet);
- try
- net.start(false);
- this.connective = net.conn(addr[0],addr[1]);
- //io.ces(this.connective,1);
- io.timeout(0);
- catch e by
- print 'Error: Cannot connect in',this.TimeOutSet/1000,'seconds';
- return -100;
- end;
- return 0;
- end
- function setHeader(headerDict)
- headers = keys(headerDict);
- for item in headers do
- if headerDict[item] = '' then
- print 'Error: Null header value' + item;
- return -3;
- end;
- this.request = this.request + item + ': ' + headerDict[item] + '\r\n';
- end;
- return 0;
- end
- function send(data)
- if this.method = 'POST' then
- this.request = this.request+ 'Content-Length: ' + len(data) + '\r\n\r\n';
- this.request = this.request + data;
- else
- this.request = this.request + '\r\n';
- end;
- io.timeout(TimeOutSet);
- try
- io.write(this.connective, this.request);
- io.wait([this.connective]);
- length = io.avail(this.connective);
- h_data = io.read(this.connective,length);
- catch e by
- print 'Error: Cannot get data in',this.TimeOutSet/1000,'seconds';
- io.close(this.connective);
- return -200;
- end;
- io.timeout(0);
- templist = split(h_data,'\r\n\r\n');
- h_data = templist[0];
- m_data = templist[1];
- inf_start = index(h_data,'content-length: ',0,true);
- if inf_start <> -1 then
- io.timeout(TimeOutSet);
- try
- inf_end = index(h_data,'\r\n',inf_start,true);
- Content_Length = num(substr(h_data,inf_start+16,inf_end - inf_start - 16));
- if Content_Length = 0 then
- return h_data;
- io.close(this.connective);
- end;
- while len(m_data) < Content_Length do
- io.wait([this.connective]);
- length = io.avail(this.connective);
- temp = io.read(this.connective,length);
- m_data = m_data + temp;
- end;
- io.close(this.connective);
- catch e by
- print 'Error: Cannot get data in',this.TimeOutSet/1000,'seconds';
- io.close(this.connective);
- return -200;
- end;
- io.timeout(0);
- return h_data + '\r\n\r\n' + m_data;
- else
- inf_start = index(h_data,'Transfer-Encoding: chunked',0,true);
- if inf_start <> -1 then
- is_end = 0;
- lines = split(m_data,'\r\n');
- for line in lines do
- if line = '0' then
- is_end = 1;
- end;
- end;
- io.timeout(TimeOutSet);
- try
- while is_end = 0 do
- io.wait([this.connective]);
- length = io.avail(this.connective);
- temp = io.read(this.connective,length);
- lines = split(temp,'\r\n');
- m_data = m_data + temp;
- for line in lines do
- if line = '0' then
- is_end = 1;
- break;
- end;
- end;
- end;
- catch e by
- print 'Error: Cannot get data in',this.TimeOutSet/1000,'seconds';
- io.close(this.connective);
- return -200;
- end;
- io.timeout(0);
- io.close(this.connective);
- return en.fromutf8(h_data + '\r\n\r\n' + m_data);
- end;
- end;
- end;
- end
- function cut(data,_start,_end,ori)
- index1 = index(data,_start,ori,true);
- if index1 = -1 then
- return -1;
- end;
- StartLength = len(_start);
- index2 = index(data,_end,index1 + StartLength,true);
- if index2 = -1 then
- return -1;
- end;
- d_start = index1 + StartLength;
- result = substr(data,d_start,index2 - d_start);
- return result;
- end
- function url_split(url)
- _host=replace(url,"http://","");
- url=split(_host,"/");
- host=url[0];
- url=replace(_host,host,"");
- return [host,url];
- end
复制代码 |