URLConnection is a class that enables Java code to access data available from various urls.
Questions tagged [urlconnection]
673 questions
27
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");
…
![](../../users/profiles/89784.webp)
Matt
- 943
- 2
- 9
- 14
23
votes
8 answers
URLConnection FileNotFoundException for non-standard HTTP port sources
I was trying to use the Apache Ant Get task to get a list of WSDLs generated by another team in our company. They have them hosted on a weblogic 9.x server on http://....com:7925/services/. I am able to get to the page through a browser, but the…
![](../../users/profiles/24367.webp)
jeffl8n
- 898
- 2
- 8
- 20
23
votes
2 answers
URLConnection is not allowing me to access data on Http errors (404,500,etc)
I am making a crawler, and need to get the data from the stream regardless if it is a 200 or not. CURL is doing it, as well as any standard browser.
The following will not actually get the content of the request, even though there is some, an…
![](../../users/profiles/68452.webp)
MichaelICE
- 9,500
- 19
- 65
- 104
23
votes
3 answers
understanding URLConnection.setReadTimeout()
Consider the following snippet:
URLConnection connection = target.openConnection();
connection.setConnectTimeout(5000); // 5 sec
connection.setReadTimeout(10000); // 10 sec
Does the connection.setReadTimeout sets the maximum time available for…
![](../../users/profiles/645854.webp)
lviggiani
- 5,214
- 8
- 45
- 79
21
votes
2 answers
Reading binary file from URLConnection
I'm trying to read a binary file from a URLConnection. When I test it with a text file it seems to work fine but for binary files it doesn't. I'm using the following mime-type on the server when the file is send out:
application/octet-stream
But so…
![](../../users/profiles/21406.webp)
Luke
- 19,180
- 30
- 102
- 166
20
votes
1 answer
URLConnection with Cookies?
I'm trying to make a URLConnection that supports cookies. According to the documentation I can use:
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
I couldn't get this code to work, then I saw this only…
![](../../users/profiles/778234.webp)
NoBugs
- 8,418
- 10
- 72
- 132
19
votes
3 answers
URLConnection does not get the charset
I'm using URL.openConnection() to download something from a server. The server says
Content-Type: text/plain; charset=utf-8
But connection.getContentEncoding() returns null. What up?
![](../../users/profiles/85821.webp)
Bart van Heukelom
- 40,403
- 57
- 174
- 291
19
votes
3 answers
What is the proper way of setting headers in a URLConnection?
My code is like the following:
URLConnection cnx = address.openConnection();
cnx.setAllowUserInteraction(false);
cnx.setDoOutput(true);
cnx.addRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT…
![](../../users/profiles/31610.webp)
Geo
- 85,654
- 109
- 315
- 497
18
votes
2 answers
How does URLConnection.setUseCaches() work in practice?
I have an Applet which is loading images over a http connection using URLConnection. I am setting setUseCaches(true) for all connections, but still not seeing any caching behavior. My image's HTTP headers have reasonable cache settings. If you look…
![](../../users/profiles/64287.webp)
Joel Carranza
- 957
- 2
- 12
- 18
18
votes
4 answers
java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
I'm writing a bit of code to upload a file from the device to the cloud over HTTPS.
Relevant snippet:
HttpsURLConnection conn = null;
URL url = new URL(urlstring);
conn = (HttpsURLConnection) url.openConnection(); // exception here.
But the cast…
![](../../users/profiles/313299.webp)
Teddy
- 1,886
- 5
- 21
- 32
17
votes
4 answers
Can I globally set the timeout of HTTP connections?
I have a program that uses javax.xml.ws.Service to call a remote service defined by a WSDL. This program runs on the Google App Engine which, by default, sets the HTTP connection timeout to 5 seconds{1}. I need to increase this timeout value since…
![](../../users/profiles/291180.webp)
Travis Webb
- 13,507
- 6
- 51
- 101
17
votes
6 answers
URLConnection or HTTPClient : Which offers better functionality and more efficiency?
I looking to create a login form for an android application. I want to use a post method to send information to the server side where it is handle by a PHP file; which in turn validates the parameters and sends back a response.
I've look through…
![](../../users/profiles/1243905.webp)
Fabii
- 3,550
- 12
- 47
- 83
17
votes
6 answers
java.io.IOException: Server returned HTTP response code: 500
I'm facing this problem with Java. I want to get some HTML informations from a URL. This code was working for so long, but suddenly, it stopped working.
When I access this URL using the browser, it opens with no problem.
The code:
URL site = new…
![](../../users/profiles/313721.webp)
rlc
- 4,921
- 5
- 36
- 46
17
votes
1 answer
Android's HttpURLConnection throws EOFException on HEAD requests
This small code snippet runs fine on my Mac's JVM. Unfortunately it crashes when executed on Android 4.2.
import java.net.HttpURLConnection;
import java.net.URL;
public class App
{
public static void main( String... arguments ) throws…
![](../../users/profiles/1493269.webp)
Taig
- 6,172
- 4
- 37
- 63
16
votes
6 answers
Instantiate Java Abstract class?
Relatively new Java programmer and I've been taught that you can't create an instance of an abstract class. I've also done a little research and I learned that in most cases when it appears an abstract class is being created, it is actually an…
![](../../users/profiles/4802608.webp)
Ausome
- 203
- 1
- 6