0

I have something set up on our server so that whenever you visit that page, if your GET request is set up right, it adds an entry to the database, marking who and when. I'm designing a system showing whether our devices are communicating, and I want to set it up so that it will automatically do that every hour.

So, if I put in something like "http://example.com/stamptime/default.aspx?playerId=3191121" into my web browser, it'll add an entry to the database.

I've been looking for a way to do that without the browser. If I open it as a web page, it works perfectly. If I try this:

URL url = new URL("http://example.com/StampTime/Default.aspx?playerId=3191121"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.connect(); 

the database doesn't get anything added to it.

Is there something I'm missing here?

4
  • 5
    Rather than write a java program to do this, it might be easier to use curl or wget. Commented Mar 19, 2015 at 17:27
  • try opening an output stream from the connection and flushing it Commented Mar 19, 2015 at 17:27
  • Or using a monitoring service that will ping the URL for you, keep track of all kinds of stats, etc. Commented Mar 19, 2015 at 17:28
  • If you're set on using Java, check out this tutorial: mkyong.com/java/how-to-send-http-request-getpost-in-java Commented Mar 19, 2015 at 17:33

1 Answer 1

2

While you most certainly can do this with the vanilla Java API, there's really no need to reinvent the wheel. Instead, use the tried and true Apache HTTP Client. It will manage the connection pooling, timeout settings and other nitty-gritty stuff for you. The sample code is available directly at the link provided, so no point in copying it here. If you're using Spring, you should consider using Spring's REST client instead.

In any case, you seem to be inserting stuff on GET requests, which is a terrible idea. GETs are meant to get stuff, not create or update. All clients, browsers included, expect GETs to be idempotent. Also, it seems like you have no security whatsoever and that anyone can insert stuff into your DB. Both of these topics are out of scope here, but be aware.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.