HttpURLConnection을 이용한 서블릿간의 데이터 수신하는 샘플예제
응용은 서로다른 WAS to WAS간의 데이터 동기화 또는 전문 통신등에 이용할수가 있다
금번 BMT진행하면서 만들었던 소스의 일부...
망할 프로 프레임 ㅡㅡ^
package com.gauce.servlet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.URL;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.net.www.protocol.http.HttpURLConnection;
public class HttpURLConnectionServlet extends HttpServlet {
private static final long serialVersionUID = 0L;
private static final long serialVersionUID = 0L;
/**
* HTTP GET 방식.
*/
protected void doGet( HttpServletRequest p_req,
HttpServletResponse p_res ) throws ServletException, IOException{
try {
p_req.setAttribute("REQUEST_TYPE", "GET");
catchService(p_req, p_res) ;
} catch (Exception le) {
throw new ServletException(le.getMessage(), le);
}
}
p_req.setAttribute("REQUEST_TYPE", "GET");
catchService(p_req, p_res) ;
} catch (Exception le) {
throw new ServletException(le.getMessage(), le);
}
}
/**
* HTTP POST 방식.
*/
protected void doPost( HttpServletRequest p_req,
HttpServletResponse p_res ) throws ServletException, IOException{
* HTTP POST 방식.
*/
protected void doPost( HttpServletRequest p_req,
HttpServletResponse p_res ) throws ServletException, IOException{
try {
p_req.setAttribute("REQUEST_TYPE", "POST");
catchService(p_req, p_res) ;
} catch (Exception le) {
throw new ServletException(le.getMessage(), le);
}
}
/**
* HTTP POST/GET 방식처리.
* @param p_req
* @param p_res
* @throws ServletException
* @throws IOException
*/
protected void catchService( HttpServletRequest p_req,
HttpServletResponse p_res ) throws ServletException, IOException {
URL url = new URL("HTTP://URL/"); // 요청을 보낸 URL
String sendData = "보낼 데이터....";
HttpURLConnection con = null;
try {
con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.connect();
// 송신할 데이터 전송.
send(con, sendData.getBytes());
int resCode = con.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
String result = read(con);
System.out.println("수신한 데이터 출력 : " + result);
} else {
throw new IOException("ERROR : Communication Error\nMSG Code : " + resCode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
con.disconnect();
}
}
/**
* 전송하는 부분
* @param p_con
* @throws IOException
*/
protected void send(HttpURLConnection p_con, byte[] p_writeMsg) throws IOException {
DataOutputStream dos = new DataOutputStream(p_con.getOutputStream());
dos.write(p_writeMsg);
dos.flush();
}
/**
* 수신하는 부분
* @param p_con
* @throws IOException
*/
protected String read(HttpURLConnection p_con) throws IOException {
DataInputStream dis = new DataInputStream(p_con.getInputStream());
int c;
StringBuffer buf = new StringBuffer();
while ((c = dis.read()) != -1) {
buf.append((char)c);
}
p_req.setAttribute("REQUEST_TYPE", "POST");
catchService(p_req, p_res) ;
} catch (Exception le) {
throw new ServletException(le.getMessage(), le);
}
}
/**
* HTTP POST/GET 방식처리.
* @param p_req
* @param p_res
* @throws ServletException
* @throws IOException
*/
protected void catchService( HttpServletRequest p_req,
HttpServletResponse p_res ) throws ServletException, IOException {
URL url = new URL("HTTP://URL/"); // 요청을 보낸 URL
String sendData = "보낼 데이터....";
HttpURLConnection con = null;
try {
con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.connect();
// 송신할 데이터 전송.
send(con, sendData.getBytes());
int resCode = con.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
String result = read(con);
System.out.println("수신한 데이터 출력 : " + result);
} else {
throw new IOException("ERROR : Communication Error\nMSG Code : " + resCode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
con.disconnect();
}
}
/**
* 전송하는 부분
* @param p_con
* @throws IOException
*/
protected void send(HttpURLConnection p_con, byte[] p_writeMsg) throws IOException {
DataOutputStream dos = new DataOutputStream(p_con.getOutputStream());
dos.write(p_writeMsg);
dos.flush();
}
/**
* 수신하는 부분
* @param p_con
* @throws IOException
*/
protected String read(HttpURLConnection p_con) throws IOException {
DataInputStream dis = new DataInputStream(p_con.getInputStream());
int c;
StringBuffer buf = new StringBuffer();
while ((c = dis.read()) != -1) {
buf.append((char)c);
}
return buf.toString();
}
}
}
}
1 comment:
블로거님 혹시 나머지 더 참고할 소스 받을 수 있을까요? 저두 님과 비슷한 업무를 받아서요... 지금 열심히 방법을 찾고 있습니다. 부탁 좀 드립니다.
Post a Comment