Skip to content

Instantly share code, notes, and snippets.

@tshrkmd
Created February 17, 2013 06:47

Revisions

  1. kerukerupappa created this gist Feb 17, 2013.
    35 changes: 35 additions & 0 deletions GetYahoo
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;

    public class GetYahoo {

    public static void main(String args[]){
    System.out.println(doGet("http://www.yahoo.co.jp/"));

    }

    //HTTP GET
    public static String doGet( String url )
    {
    try
    {
    HttpGet method = new HttpGet( url );
    DefaultHttpClient client = new DefaultHttpClient();

    HttpResponse response = client.execute( method );
    int status = response.getStatusLine().getStatusCode();
    if ( status != HttpStatus.SC_OK ) {
    return "エラー";
    }

    return EntityUtils.toString( response.getEntity(), "UTF-8" );
    }
    catch ( Exception e )
    {
    return e.getMessage();
    }
    }
    }