0

As the title states, I'm attempting to simply send a HTTP request to a webserver with URLConnection, but when I attempt to run the script, it does not send the request, nor does it give me an error.

Code:

public static URLConnection connection;
new URL("http://website.com/dostuff.php?whatever=" + whatever).openConnection();
1
  • The request might not actually be sent until you try to access the status code or streams. Commented Jul 22, 2013 at 20:28

1 Answer 1

1

URL.openConnection does not create the connection immediately. It only connects after you call a method that requires a connection. For example:

URL url = ...;
InputStream in = url.openStream(); // connection happens here
// you can now read the output from in
Sign up to request clarification or add additional context in comments.

4 Comments

I just tried openConnection().connect(); and it still did not throw an error or work.
Then try openConnection().getInputStream() and discard the result. Alternatively, use openStream() for less code, it does the same thing.
Can I get a code snippet to help me out? I'm new with this function =[
I posted a code snippet in the answer. That assumes you also want the output of the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.