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
44
votes
2 answers

Sending a JSON HTTP POST request from Android

I'm using the code below to send an http POST request which sends an object to a WCF service. This works ok, but what happens if my WCF service needs also other parameters? How can I send them from my Android client? This is the code I've written…
Libathos
  • 3,340
  • 5
  • 36
  • 61
43
votes
4 answers

Android download PDF from URL then open it with a PDF reader

I am trying to write an app to download PDFs from a URL, store them on SD, then open by Adobe PDF reader or other apps (which ever is able to open the PDF). Until now, I had "successfully downloaded and stored it on SD card" (but every time when I…
sefirosu
  • 2,558
  • 7
  • 44
  • 69
40
votes
4 answers

Android HttpsUrlConnection eofexception

I have a problem where my HttpsURLConnection will throw an EOFException when i try to read any input. The code works for some network calls, but fails on others. If i try and read anything from the connection, it fails with the aforementioned…
user1883083
  • 841
  • 1
  • 8
  • 10
38
votes
2 answers

How to send Https Post request in java

I want to login to application from java code. Here is my code... String httpsURL = "https://www.abcd.com/auth/login/"; String query = "email="+URLEncoder.encode("abc@xyz.com","UTF-8"); query += "&"; query +=…
Santhosh
  • 4,956
  • 12
  • 62
  • 90
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
36
votes
2 answers

HttpURLConnection to Send image , audio and video files with parameter may (String or Json String) Android

I'm sharing the solution to send an image , audio or a video file with parameters using HttpURLConnection. Parameters may be (plain string or JSON). (Android Client to PHP Back end) Scenario: Have to upload media files (audio , video and…
Amir
  • 1,066
  • 1
  • 13
  • 26
36
votes
1 answer

Two way sync for cookies between HttpURLConnection (java.net.CookieManager) and WebView (android.webkit.CookieManager)

Unfortunately, there's a multitude of cookie managers for Android. The cookies for HttpURLConnection are maintained by java.net.CookieManager and the cookies for WebView are maintained by android.webkit.CookieManager. These cookie repositories are…
talkol
  • 12,564
  • 11
  • 54
  • 64
35
votes
4 answers

IOException: "Received authentication challenge is null" (Apache Harmony/Android)

I am trying to send a GET via Android's HttpURLConnection (imported from org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection), and upon receiving the response, an IOException is thrown: in doRequestInternal(): "Received…
mxk
  • 43,056
  • 28
  • 105
  • 132
34
votes
4 answers

Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager)

I've seen answers about how this should work with the old DefaultHttpClient but there's not a good example for HttpURLConnection I'm using HttpURLConnection to make requests to a web application. At the start of the my Android application, I use…
quietmint
  • 13,885
  • 6
  • 48
  • 73
33
votes
2 answers

How to handle HTTP authentication using HttpURLConnection?

I'm writing a Java client that POSTs to a HTTP server that requires authentication. I have to support at least the following three authentication methods: Basic, Digest or Negotiate. Additionally the POST may be very large (over 2MB), so I need to…
Opher
  • 525
  • 1
  • 4
  • 11
33
votes
1 answer

How to Reuse HttpUrlConnection?

I am interested in reusing an HttpUrlConnection (as part of a statefull protocol between server and client that I'm developing). I know that there is an Connection=keep-alive header for persistent http. Now, I want to know how to reuse such a…
user967710
  • 1,815
  • 3
  • 32
  • 58
33
votes
8 answers

Why am I getting Premature End of File Error?

I am trying to parse an XML response, but I am failing miserably. I thought initially that the xml was just not being returned in the response, so I crafted up the code below with a direct link to my xml file online. I am able to print the XML to…
Fabii
  • 3,820
  • 14
  • 51
  • 92
32
votes
12 answers

java.io.IOException: unexpected end of stream on Connection in android

I have web service URL, it working fine. It gives the JSON data. When I am using HttpURLConnection and InputStream, I am getting this error: java.io.IOException: unexpected end of stream on Connection{comenius-api.sabacloud.com:443,…
32
votes
6 answers

HttpUrlConnection.openConnection fails second time

I know this issue should be fixed with System.setProperty("http.keepAlive", "false"); before openConnection, but that didn't work to me. First try on this code works, second one fails. Even if i try this request after less than 5 seconds, it also…
ggomeze
  • 5,711
  • 6
  • 29
  • 32
32
votes
7 answers

Android HttpUrlConnection EOFException

I would like to know if there are known issues on Android with HttpUrlConnection and POST requests. We are experiencing intermittent EOFExceptions when making POST requests from an Android client. Retrying the same request will eventually work. Here…
Matt Accola
  • 4,090
  • 4
  • 28
  • 37