Created
May 16, 2013 02:44
-
-
Save seasu/5589038 to your computer and use it in GitHub Desktop.
Java using HttpURLConnection to get or post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String excuteGet(String desiredUrl) | |
throws Exception { | |
URL url = null; | |
BufferedReader reader = null; | |
StringBuilder stringBuilder; | |
try { | |
// create the HttpURLConnection | |
url = new URL(desiredUrl); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
// just want to do an HTTP GET here | |
connection.setRequestMethod("GET"); | |
// uncomment this if you want to write output to this url | |
//connection.setDoOutput(true); | |
// give it 15 seconds to respond | |
connection.setReadTimeout(15 * 1000); | |
connection.connect(); | |
// read the output from the server | |
reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
stringBuilder = new StringBuilder(); | |
String line = null; | |
while ((line = reader.readLine()) != null) { | |
stringBuilder.append(line + "\n"); | |
} | |
return stringBuilder.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw e; | |
} finally { | |
// close the reader; this can throw an exception too, so | |
// wrap it in another try/catch block. | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
} | |
} | |
} | |
/* | |
* The urlParameters is a URL encoded string. | |
* | |
* String urlParameters = "fName=" + URLEncoder.encode("???", "UTF-8") + | |
* "&lName=" + URLEncoder.encode("???", "UTF-8") | |
*/ | |
public static String excutePost(String targetURL, String urlParameters) { | |
URL url; | |
HttpURLConnection connection = null; | |
try { | |
//Create connection | |
url = new URL(targetURL); | |
connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Content-Type", | |
"application/x-www-form-urlencoded"); | |
connection.setRequestProperty("Content-Length", "" | |
+ Integer.toString(urlParameters.getBytes().length)); | |
connection.setRequestProperty("Content-Language", "en-US"); | |
connection.setUseCaches(false); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
//Send request | |
DataOutputStream wr = new DataOutputStream( | |
connection.getOutputStream()); | |
wr.writeBytes(urlParameters); | |
wr.flush(); | |
wr.close(); | |
//Get Response | |
InputStream is = connection.getInputStream(); | |
BufferedReader rd = new BufferedReader(new InputStreamReader(is)); | |
String line; | |
StringBuilder response = new StringBuilder(); | |
while ((line = rd.readLine()) != null) { | |
response.append(line); | |
response.append('\r'); | |
} | |
rd.close(); | |
return response.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} finally { | |
if (connection != null) { | |
connection.disconnect(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment