package com.mckay.proxy; import java.io.FileDescriptor; import java.net.HttpURLConnection; import java.net.URLConnection; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.OutputStream; import java.io.InputStream; import java.net.URL; import java.net.MalformedURLException; import java.io.*; class getter extends Thread { InputStream in = null; OutputStream out = (OutputStream)System.out; HttpURLConnection get = null; byte[] bytes = new byte[1500]; public getter(URL url) { try { get = (HttpURLConnection)url.openConnection(); get.setDoOutput(false); get.setDoInput(true); in = get.getInputStream(); } catch (IOException ioe) { ioe.printStackTrace(); } } public String getCookie() { return get.getHeaderField("Set-Cookie"); } // the actual thread.. does copying. public void run() { int i = 0; try { while ((i=in.read(bytes))>0) { out.write(bytes,0,i); out.flush(); } } catch (IOException ioe) { ioe.printStackTrace(); } } } class poster extends Thread { OutputStream out = null; byte[] bytes = new byte[1500]; InputStream in = System.in; String cookie = null; public poster(URL url, String sessioncookie) { HttpURLConnection post = null; cookie = sessioncookie; try { post = (HttpURLConnection)url.openConnection(); post.setUseCaches(false); post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); post.setRequestProperty("Cookie", cookie); post.setFixedLengthStreamingMode(1000000000); post.setAllowUserInteraction(false); post.setDoInput(false); post.setDoOutput(true); out = post.getOutputStream(); } catch (IOException ioe) { ioe.printStackTrace(); } } // this is the thread... public void run() { int i = 0; try { while ((i=in.read(bytes))>0) { out.write(bytes,0,i); out.flush(); } } catch (IOException ioe) { ioe.printStackTrace(); } } } public class HttpTunnelClient extends Thread { public static final String BASEURL = "http://localhost:8081/pixel/datafeed"; public static void main(String args[]) { URL url = null; try { url = new URL(BASEURL + "?host=127.0.0.1&port=9990"); } catch (MalformedURLException mue) { mue.printStackTrace(); } if (url!=null) { getter g1 = new getter(url); g1.start(); String cookie = g1.getCookie(); // System.out.println("Got cookie: " + cookie ); poster p1 = new poster(url,cookie); p1.start(); try { g1.join(); } catch (InterruptedException ie) { ie.printStackTrace(); } } } }