URLConnection is a class that enables Java code to access data available from various urls.
Questions tagged [urlconnection]
674 questions
2122
votes
12 answers
How to use java.net.URLConnection to fire and handle HTTP requests
Use of java.net.URLConnection is asked about pretty often here, and the Oracle tutorial is too concise about it.
That tutorial basically only shows how to fire a GET request and read the response. It doesn't explain anywhere how to use it to, among…

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
145
votes
5 answers
Can you explain the HttpURLConnection connection process?
I am using HTTPURLConnection to connect to a web service. I know how to use HTTPURLConnection but I want to understand how it works. Basically, I want to know the following:
On which point does HTTPURLConnection try to establish a connection to the…

Arci
- 6,647
- 20
- 70
- 98
104
votes
11 answers
Unable to resolve host "" No address associated with hostname
I tried following this tutorial:
Getting Data from the Web
I tried implementing it on Android 3.0, the latest platform for tablets, however, I get this error:
Unable to resolve host "www.anddev.org" No address associated with
hostname.
You can…

황현정
- 3,451
- 6
- 28
- 35
93
votes
10 answers
Why would a "java.net.ConnectException: Connection timed out" exception occur when URL is up?
I'm getting a ConnectException: Connection timed out with some frequency from my code. The URL I am trying to hit is up. The same code works for some users, but not others. It seems like once one user starts to get this exception they continue to…

Sarah Haskins
- 931
- 1
- 8
- 3
93
votes
4 answers
What exactly does URLConnection.setDoOutput() affect?
There's setDoOutput() in URLConnection. According to documentation I should
Set the DoOutput flag to true if you intend to use the URL connection for output, false if not.
Now I'm facing exactly this problem - the Java runtime converts the…

sharptooth
- 167,383
- 100
- 513
- 979
81
votes
8 answers
Upload files from Java client to a HTTP server
I'd like to upload a few files to a HTTP server. Basically what I need is some sort of a POST request to the server with a few parameters and the files. I've seen examples of just uploading files, but didn't find how to also pass additional…

Marius
- 3,976
- 5
- 37
- 52
68
votes
3 answers
Convenient way to parse incoming multipart/form-data parameters in a Servlet
Is there any convenient way to read and parse data from incoming request.
E.g client initiate post request
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",…

atuser
- 683
- 1
- 5
- 4
66
votes
5 answers
Java URLConnection Timeout
I am trying to parse an XML file from an HTTP URL. I want to configure a timeout of 15 seconds if the XML fetch takes longer than that, I want to report a timeout. For some reason, the setConnectTimeout and setReadTimeout do not work. Here's the…

Chris
- 3,787
- 13
- 42
- 49
53
votes
3 answers
Apache HTTP client or URLConnection
I need to download a web page on an Android app and I am having a hard time deciding whether to use the Android Apache HTTP client or Java's URLConnection.
Any thoughts?

Amit Raz
- 5,370
- 8
- 36
- 63
41
votes
2 answers
Difference between URLConnection, HttpURLConnection and HttpsURLConnection
What is the difference between URLConnection, HttpURLConnection and HttpsURLConnection (with SSL). Under what conditions, which one should I use?

Questions
- 20,055
- 29
- 72
- 101
39
votes
4 answers
Getting "java.net.ProtocolException: Server redirected too many times" Error
I'm making a simple URL request with code like this:
URL url = new URL(webpage);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();
But on that last line, I'm getting the "redirected too many times…

rjcarr
- 2,072
- 2
- 22
- 35
38
votes
8 answers
Java: how to use UrlConnection to post request with authorization?
I would like to generate POST request to a server which requires authentication. I tried to use the following method:
private synchronized String CreateNewProductPOST (String urlString, String encodedString, String title, String content, Double…

Niko Gamulin
- 66,025
- 95
- 221
- 286
37
votes
2 answers
Java URLConnection - When do I need to use the connect() method?
I have a problem to understand the meaning of the connect() method in the URLConnection class. In the following code, if I use the connect() method, I get the same result if I don't use it.
Why (or when) do I need to use it?
URL u = new…

kappa
- 520
- 1
- 5
- 11
30
votes
1 answer
HttpURLConnection sends a POST request even though httpCon.setRequestMethod("GET"); is set
Here is my code:
String addr = "http://172.26.41.18:8080/domain/list";
URL url = new URL(addr);
HttpURLConnection httpCon = (HttpURLConnection)…

Lesya Makhova
- 1,340
- 3
- 14
- 28
30
votes
3 answers
Can I override the Host header where using java's HttpUrlConnection class?
I'm using the following code to open a http connection in java:
URL url = new URL("http://stackoverflow.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
…

Matt
- 983
- 2
- 9
- 14