Skip to content

Instantly share code, notes, and snippets.

@wagurano
Created March 16, 2013 16:36
Show Gist options
  • Save wagurano/5177209 to your computer and use it in GitHub Desktop.
Save wagurano/5177209 to your computer and use it in GitHub Desktop.
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
/**
*
*
*/
public class Request {
public static void main(String args[]) {
testEPOST();
}
public static void testEPOST() {
String u = "http://openapi.epost.go.kr/postal/retrieveLotNumberAdressService/"
+ "retrieveLotNumberAdressService/getComplexList?ServiceKey="
+ "2zqJV8fgr%2BJFC6UecKgnO9uBAe0fQwJG3fZUsuMbiaQlVhxeSlLKtMhk5KOj6zz"
+ "%2BfXfgOVgi1OANTbWyHsf%2FIQ%3D%3D&srchwrd="
+ "미수"
+ "&areaNm="
+ "강동구"
+ "&searchSe=or";
Request request = null;
String aLine = null;
request = new Request(u);
request.open();
byte[] bu = new byte[1028];
int len = request.readBytes(bu);
try {
System.out.println(new String(bu,0,len,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
request.close();
}
protected URL url = null;
protected URLConnection connection = null;
protected InputStream isReceive = null;
protected BufferedInputStream bisReceive = null;
protected InputStreamReader isrReceive = null;
protected BufferedReader brReceive = null;
protected OutputStream osSend = null;
protected OutputStreamWriter oswSend = null;
protected int bufferPosition = -1;
public Request(String address) {
try {
url = new URL(address);
connection = url.openConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean open() {
try {
isReceive = connection.getInputStream();
bisReceive = new BufferedInputStream(isReceive);
isrReceive = new InputStreamReader(isReceive);
brReceive = new BufferedReader(isrReceive);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public int connect() {
try {
isReceive = connection.getInputStream();
bisReceive = new BufferedInputStream(isReceive);
isrReceive = new InputStreamReader(isReceive);
brReceive = new BufferedReader(isrReceive);
} catch (Exception e) {
String msg = e.getMessage();
int i = -1;
if (-1 < (i = msg.indexOf("response code"))) {
return Integer.parseInt(msg.substring(i + 15, i + 18));
}
return 999;
}
return 0;
}
public boolean post(String param) {
if (null != connection) {
try {
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr");
osSend = connection.getOutputStream();
oswSend = new OutputStreamWriter(osSend);
oswSend.write(param);
oswSend.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
public boolean send(String param) {
if (null != connection) {
try {
connection.setDoOutput(true);
connection.setRequestProperty("content-type", "text/xml;charset=euc-kr");
osSend = connection.getOutputStream();
oswSend = new OutputStreamWriter(osSend);
oswSend.write(param);
oswSend.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
public String getCookie() {
if (null != connection) {
return connection.getHeaderField("Set-Cookie");
} else {
return null;
}
}
public void setCookie(String c) {
if (null != connection) {
connection.addRequestProperty("Cookie", c);
}
}
public boolean close() {
try {
if (null != brReceive) {
brReceive.close();
brReceive = null;
}
if (null != isrReceive) {
isrReceive.close();
isrReceive = null;
}
if (null != bisReceive) {
bisReceive.close();
bisReceive = null;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public String readLine() {
try {
if (null != brReceive) {
return brReceive.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public int readBytes(byte b[]) {
try {
if(null != bisReceive) {
return bisReceive.read(b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment