java httpurlconnection post example
how to make http connection - get and post (can login info.kw.ac.kr)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>.... ....</title>
</head>
<body>
<%
Date now = new Date();
BufferedReader oBufReader = null;
HttpURLConnection httpConn = null;
String strBuffer = "";
String strRslt = "";
String id = "";
String pw = "";
String query = "";
try
{
String strEncodeUrl = "";
URL oOpenURL = new URL(strEncodeUrl);
httpConn = (HttpURLConnection) oOpenURL.openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setRequestProperty("Content-Length", String.valueOf(query.getBytes().length));
//httpConn.setRequestProperty("Referer", "");
//httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36");
//DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
writer.write(query);
writer.flush();
oBufReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
//Buffer. .. ... .. ... ... .....
while((strBuffer = oBufReader.readLine()) != null)
{
if(strBuffer.length() > 1)
{
strRslt += strBuffer;
}
}
System.out.println(now + " " + strRslt);
if(strRslt.indexOf("location.replace") > 1)
{
strRslt = "..";
}else{
strRslt = "..";
}
oBufReader.close();
httpConn.disconnect();
} catch( Exception ee) {
ee.getMessage();
}
%>
<%=strRslt %> <br/> <%=id %> <br /> <%=pw %>
</body>
</html>
`