Skip to content

Instantly share code, notes, and snippets.

@lu911
Created March 15, 2014 09:40
Show Gist options
  • Save lu911/9564117 to your computer and use it in GitHub Desktop.
Save lu911/9564117 to your computer and use it in GitHub Desktop.
public String POST(String addr, String post){
StringBuffer sb = null;
String type = "application/x-www-form-urlencoded";
HttpURLConnection urlCon = null;
BufferedReader br = null;
OutputStreamWriter wr = null;
String line = null;
try {
sb = new StringBuffer();
URL url = new URL(addr);
urlCon = (HttpURLConnection) url.openConnection();
urlCon.setRequestProperty("Content-Type", type);
urlCon.setRequestMethod("POST");
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.connect();
wr = new OutputStreamWriter(urlCon.getOutputStream(),"utf8");
wr.write(post);
wr.flush();
br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "utf8"));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
Log.d("fuck","EDONG =" + e.getMessage());
}
finally{
if(urlCon != null){
urlCon.disconnect();
}
if(wr != null){
try {
wr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sb.toString();
}
public String GET(String addr){
StringBuffer sb = null;
BufferedReader br = null;
HttpURLConnection urlCon = null;
String line = null;
try {
sb = new StringBuffer();
URL url = new URL(addr);
urlCon = (HttpURLConnection) url.openConnection();
urlCon.setRequestMethod("GET");
urlCon.setDoInput(true);
urlCon.connect();
br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"utf-8"));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(urlCon != null){
urlCon.disconnect();
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment