import java.net.*; import java.io.*; import java.lang.Thread ; public class URLFork implements Runnable { public String url ; public int alive ; public String sts= "OK" ; public String m_ErrorInfo="" ; public int rTime = 0 ; public int cTime = 0 ; public ByteArrayOutputStream m_bais =null ; public int rCode=0 ; public HttpURLConnection proConn =null ; public URLFork(String url) { this.url = url ; this.alive=0 ; this.sts="OK" ; this.m_ErrorInfo=""; this.m_bais =null ; this.proConn = null; } public URLFork(String url,int l_cTime,int l_rTime) { this.url = url ; this.alive=0 ; this.sts="OK" ; this.m_ErrorInfo=""; this.m_bais =null ; this.proConn = null; this.cTime=l_cTime ; this.rTime=l_rTime ; this.rCode=0; } public void setReadTimeOut(int l_rTime) { this.rTime=l_rTime; } public void setConnectTimeOut(int l_cTime) { this.cTime=l_cTime; } public void startfork() { this.alive=1 ; new Thread(this).start(); } public void run() { try { alive=1 ; URL proreports = new URL(url); proConn = (HttpURLConnection) proreports.openConnection(); if (this.cTime>0) proConn.setConnectTimeout(this.cTime); if (this.rTime>0) proConn.setReadTimeout(this.rTime) ; this.rCode=proConn.getResponseCode(); //System.out.println(proConn.getReadTimeOut()); DataInputStream dis =null ; if (this.rCode!=proConn.HTTP_OK) dis = new DataInputStream(proConn.getErrorStream()); else dis = new DataInputStream(proConn.getInputStream()); m_bais = new ByteArrayOutputStream(); byte[] byteChunk = new byte[4096]; int n; while ( (n = dis.read(byteChunk)) > 0 ) { m_bais.write(byteChunk, 0, n); } dis.close(); alive=0; } catch (MalformedURLException me) { alive=0 ; sts="ERROR" ; if (this.rCode==0) this.rCode=-1; //this.m_bais =null ; m_ErrorInfo="MalformedURLException: " + me.getMessage(); } catch (IOException ioe) { alive=0 ; sts="ERROR" ; if (this.rCode==0) this.rCode=-2; //this.m_bais =null ; m_ErrorInfo="IOException: " + ioe.getMessage(); } catch (Exception e) { alive=0 ; sts="ERROR" ; if (this.rCode==0) this.rCode=-3; //this.m_bais =null ; m_ErrorInfo="Exception: " + e.getMessage(); } } public String getHeaderField(String n) { return this.proConn.getHeaderField(n); } public byte[] getOutput() { if (this.m_bais==null) return new String("").getBytes(); return this.m_bais.toByteArray(); } public byte[] getBytes() { if (this.m_bais==null) return new String("").getBytes(); return this.m_bais.toByteArray(); } public String getErrInfo() { return this.m_ErrorInfo ; } public String getStatus() { return this.sts ; } public int getResponseCode() { return this.rCode; } public int isForkAlive() { return this.alive ; } public static void main(String[] args) { System.out.println("URLFork v.1.3"); } }