JOEZACK.COM Code Musings and Such

9Jun/090

How to issue a simple HTTPRequest in Java

I needed a way to issue an HTTPRequest in Java, and I don't care about the return value. I wasn't able to find a simple example so I thought I'd post my that part of my solution. I don't know that it's the best way, but it works.

import java.net.URL;

public class Request {
  public static void main(String[] args) throws Exception {
      if(args.length == 0) {
          System.out.println("URL required.");
          return;
      }
      URL url = new URL(args[0]);
      url.openStream().close();
  }
}

And to use it you just pass in the url via the command line

java Request http://joezack.com
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged as: Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.