Created
December 15, 2011 08:53
-
-
Save ryoco/1480418 to your computer and use it in GitHub Desktop.
test httpconnection java
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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
public class HttpConnectionSample | |
{ | |
public static void main(String[] args) | |
{ | |
HttpURLConnection connection = null; | |
try{ | |
URL url = new URL("http://www.scala-lang.org/"); | |
connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.setDoOutput(true); | |
connection.setReadTimeout(1000); | |
connection.connect(); | |
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
StringBuilder sb = new StringBuilder(); | |
String line = null; | |
while ( (line = r.readLine()) != null){ | |
sb.append(line+ '\n'); | |
} | |
r.close(); | |
System.out.println(sb.toString()); | |
}catch (Exception e){ | |
System.out.println(String.format("Error is %s",e)); | |
e.printStackTrace(); | |
} | |
finally{ | |
connection.disconnect(); | |
connection = null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment