Obtaining an HTTP document

Another way to use the URL class is to download a document from an HTTP URL. When you start the client connects to a Web server. In the client code, you:

The following code sample works by:

import java.io.*;
import java.net.*;
public class URLprocess {
		public static InputStream readURL() 
			throws Exception {
			URL u = newURL(“http://www.xxxx.com”);
		InputStream in = u.openStream();
//This is the same as creating URLConnection, then //calling getInputStream(). In ASE you need to read 
//the entire document into memory, then create an //InputStream on the in-memory copy.
		int n=0,off;
		byte b[]=new byte[50000];
		for(off=0;(off<b.length512)
			&&((n=in.read(b,off,512)!=-1);off+=n){}
System.out.println(“Number of bytes read :” + off);
	in.close();
ByteArrayInputStream test = 
	new ByteArrayInputStream(b,0,off);
return (InputStream) test;
		}
}

After you create the new InputStream class, you can install this class and use it to read a text file into the database, inserting data into a table, as in the following example.

create table t (cl text)
go

insert into t values (URLprocess.readURL())
go
Number of bytes read :40867

select datalength(cl) from t
go
------------
 		40867