Skip to content

Instantly share code, notes, and snippets.

@ryoco
Created December 15, 2011 08:53
Show Gist options
  • Save ryoco/1480418 to your computer and use it in GitHub Desktop.
Save ryoco/1480418 to your computer and use it in GitHub Desktop.
test httpconnection java
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