Questions tagged [httpurlconnection]

HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances.

The HttpURLConnection class is used to send and receive data over the web using the HTTP protocol. Data may be of any type and length.

Uses of this class follow a pattern:

  • Obtain a new HttpURLConnection by calling URL.openConnection() and casting the result to HttpURLConnection.
  • Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types and session cookies.
  • Optionally upload a request body. Instances must be configured with setDoOutput(true) if they include a request body. Transmit data by writing to the stream returned by getOutputStream().
  • Read the response. Response headers typically include metadata such as the response body's content type and length, modified dates and session cookies. The response body may be read from the stream returned by getInputStream(). If the response has no body, that method returns an empty stream.
  • Disconnect. Once the response body has been read, the HttpURLConnection should be closed by calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed or reused.

For more information visit HttpURLConnection.

3261 questions
98
votes
10 answers

Getting java.net.SocketTimeoutException: Connection timed out in android

I'm developing an android application where I'm sending requests to the web server and parsing JSON objects. Frequently I'm getting java.net.SocketTimeoutException: Connection timed out exception while communicating with the server. Some times it…
akh
  • 2,478
  • 3
  • 23
  • 23
79
votes
6 answers

Add Header Parameter in Retrofit

I'm trying to call an API which requires me to pass in an API key. My Service call using HttpURLConnection is working perfectly. url = new URL("https://developers.zomato.com/api/v2.1/search?entity_id=3&entity_type=city&q=" +…
jamian
  • 1,544
  • 2
  • 16
  • 25
72
votes
6 answers

Android download binary file problems

I am having problems downloading a binary file (video) in my app from the internet. In Quicktime, If I download it directly it works fine but through my app somehow it get's messed up (even though they look exactly the same in a text editor). Here…
Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
72
votes
9 answers

java.net.SocketTimeoutException: timeout

With OkHttp library, application is facing following SocketTimeoutException issue. If request size is less, then it is working fine(Less than 1MB). I am getting this exception within 10 seconds, even my socket timeout(readTimeout) value is much…
Vivek
  • 4,170
  • 6
  • 36
  • 50
66
votes
5 answers

Parse JSON from HttpURLConnection object

I am doing basic http auth with the HttpURLConnection object in Java. URL urlUse = new URL(url); HttpURLConnection conn = null; conn = (HttpURLConnection) urlUse.openConnection(); conn.setRequestMethod("GET"); …
CQM
  • 42,592
  • 75
  • 224
  • 366
59
votes
10 answers

How to check if InputStream is Gzipped?

Is there any way to check if InputStream has been gzipped? Here's the code: public static InputStream decompressStream(InputStream input) { try { GZIPInputStream gs = new GZIPInputStream(input); return gs; } catch…
voo
  • 1,293
  • 1
  • 12
  • 18
56
votes
8 answers

Java simple code: java.net.SocketException: Unexpected end of file from server

I wrote some simple code in Java, the method should connect to the website and return the BufferedReader. private BufferedReader getConnection(String url_a) { URL url; try { System.out.println("getting connection"); …
Maciej Skrzypiński
  • 557
  • 1
  • 4
  • 10
56
votes
8 answers

How to enable wire logging for a java HttpURLConnection traffic?

I've used Jakarta commons HttpClient in another project and I would like the same wire logging output but using the "standard" HttpUrlConnection. I've used Fiddler as a proxy but I would like to log the traffic directly from java. Capturing what…
Serxipc
  • 6,639
  • 9
  • 40
  • 51
55
votes
2 answers

Android HTTPUrlConnection : how to set post data in http body?

I've already created my HTTPUrlConnection : String postData = "x=val1&y=val2"; URL url = new URL(strURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type",…
Rob
  • 15,732
  • 22
  • 69
  • 107
51
votes
2 answers

How to set Content Type on HttpURLConnection?

Do you know how to set Content-Type on HttpURLConnection? Following code is on Blackberry and I want the Android equivalent: connection.setRequestProperty("content-type", "text/plain; charset=utf-8"); connection.setRequestProperty("Host",…
AndroiDBeginner
  • 3,571
  • 11
  • 40
  • 43
48
votes
3 answers

simple HttpURLConnection POST file multipart/form-data from android to google blobstore

I have very little idea how html works.What i want to do is exactly similar to the following but on android
48
votes
3 answers

HttpURLConnection java.io.FileNotFoundException

The code below works great if I connect to what seems to be Apache servers, however when I try to connect to my .Net server it throws an error. I am guessing it is a header requirement, but I can not seem to get a successful response no matter what…
nathan
  • 477
  • 1
  • 4
  • 8
45
votes
4 answers

FileNotFoundException for HttpURLConnection in Ice Cream Sandwich

I have an Android app that works fine with Android 2.x and 3.x, but it fails when run on Android 4.x. The problem is in this section of code: URL url = new URL("http://blahblah.blah/somedata.xml"); HttpURLConnection urlConnection =…
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
45
votes
3 answers

HttpEntity is deprecated on Android now, what's the alternative?

With the release of Android 5.1, it looks like all the Apache http stuff has been deprecated. Looking at the documentation is useless; they all say This class was deprecated in API level 22. Please use openConnection() instead. Please visit this…
joshkendrick
  • 3,497
  • 8
  • 38
  • 52
45
votes
5 answers

How to handle cookies in httpUrlConnection using cookieManager

I have a server request that returns multiple cookies, like that: This is how I'm storing these cookies to the cookieManager: HttpURLConnection connection = ... ; static java.net.CookieManager msCookieManager = new…
David
  • 37,109
  • 32
  • 120
  • 141