*
*/
package jijo;
import com.client. HttpTunnel;
/**
* @author jijo
*
*/
public class Tunnel {
public static void main(String args[]){
Object obj = "grgt";
Object result = HttpTunnel.transaction((Object)obj, "http://192.168.0.123/tunnel/myservlet.do");
}
}
package com.client;
/*
* File: HttpTunnel.java
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
public class HttpTunnel {
static public Object transaction(Object data, String url) {
URL server = null;
URLConnection con=null;
HttpURLConnection con1=null;
try {
server = new URL(url);
} catch(MalformedURLException e) {
System.out.println("URL exception: " + e );
return null;
}
// send request
ObjectInputStream response = null;
Object result = null;
try {
con1 = (HttpURLConnection) server.openConnection();
con1.setDoOutput(true);
con1.setUseCaches(false);
con1.setRequestMethod("POST");
con1.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream request =
new ObjectOutputStream(
new BufferedOutputStream(con1.getOutputStream()));
request.writeObject(data);
request.flush();
request.close();
// get the result input stream
response = new ObjectInputStream(new BufferedInputStream(con1.getInputStream()));
// read response back from the server
result = response.readObject();
} catch(IOException ignored) {
ignored.printStackTrace();
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
finally {
try {
con1=null;
server=null;
if( response!=null ) {
response.close();
}
} catch(IOException ex) {}
}
return result;
}
}
/************************************/
/**
*
*/
package jijo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author jijo
*
*/
public class MyServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void init (ServletConfig c) throws ServletException {
super.init (c);
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException{
try{
ObjectInputStream inputStream = new ObjectInputStream(new BufferedInputStream(request.getInputStream()));
Object o = null;
o = inputStream.readObject();
if( o!=null && (o instanceof Object) ) {
System.out.println("Object : "+o);
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream (response.getOutputStream ()));
response.setStatus(HttpServletResponse.SC_OK);
oos.writeObject ("OK");
oos.flush ();
oos.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/****************************************/
in the web.xml
No comments:
Post a Comment