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
18
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…
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
18
votes
6 answers

Java - HttpUrlConnection returns cached response every time

I'm trying to gather statistical data from Roblox's currency exchange for analysis. Therefore, I need up-to-date data instead of a cached result. However, it seems that no matter what I do, the result is still cached. It seems that the most…
18
votes
4 answers

Maintain session between HttpUrlConnection Calls (Native/Webview)

Let me start with what I desire: I want to make an app which is part native and part webviews. Problem - Maintain a session between native and webview parts. My Approach to handle this: I intend to implement a native login, in which I present…
User3
  • 2,465
  • 8
  • 41
  • 84
18
votes
4 answers

"Illegal State Exception: Already Connected" when using HttpURLConnection

I get an illegal state exception when i set DoOutput to true. public boolean sendLinksToMaster(String ipport, List links){ boolean sent = false; String[] tokens = ipport.split(":"); String data =…
arpitpanwar
  • 189
  • 1
  • 1
  • 8
18
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…
Taig
  • 6,718
  • 4
  • 44
  • 65
17
votes
3 answers

HttpURLConnection.getResponseCode() throws IOException when code is known

How come HttpURLConnection.getResponseCode() throws IOException even if the status is known? Caused by: java.io.IOException: Server returned HTTP response code: 412 for URL: It's not a problem of getting the response code because it is…
danieln
  • 4,795
  • 10
  • 42
  • 64
16
votes
2 answers

Java Authenticator on a per connection basis?

I'm building an Eclipse plugin that talks to a REST interface which uses Basic Authentication. When the authentication fails I would like to popup my plugin's settings dialog and retry. Normally I could use the static Authenticator.setDefault() to…
Martijn Laarman
  • 13,476
  • 44
  • 63
16
votes
2 answers

Android POST request with JSON

After researches i still cant send a JSON POST request to a server. I already tried some older answers: Java - sending HTTP parameters via POST method easily [Android]-POST Json with HttpUrlConnection Post request for registering user data on server…
Cihat
  • 835
  • 1
  • 7
  • 17
16
votes
2 answers

What could cause socket ConnectException: Connection timed out?

We have a Webstart client that communicates to the server by sending serialized objects over HTTPS using java.net.HttpsURLConnection. Everything works perfectly fine on my local machine and on test servers located in our office, but I'm experiencing…
ColinD
  • 108,630
  • 30
  • 201
  • 202
16
votes
4 answers

java.io.IOException: unexpected end of stream

getting issue when writing large video file using httpurlconnection. java.io.IOException: unexpected end of stream on Connection{192.1.4.55, proxy=DIRECT@ hostAddress=192.1.4.55 cipherSuite=none protocol=http/1.1} (recycle count=0) W/System.err: …
JosephM
  • 2,935
  • 4
  • 18
  • 28
16
votes
6 answers

HttpURLConnection implementation

I have read that HttpURLConnection supports persistent connections, so that a connection can be reused for multiple requests. I tried it and the only way to send a second POST was by calling openConnection for a second time. Otherwise I got a…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
16
votes
2 answers

Android allow multiple file upload (Max 150 MB) to PHP Server

I have to allow a user to upload multiple files(can be image/video/audio)in a single request from my android application to the PHP server. I am using REST web service. For this functionality, I am using the following code: /* To upload the multiple…
Manali Sheth
  • 379
  • 2
  • 5
  • 17
16
votes
2 answers

several requests from one HttpURLConnection

How can I do several request in one HttpURLConnection with Java? URL url = new URL("http://my.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); HttpURLConnection.setFollowRedirects( true ); connection.setDoOutput(…
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
16
votes
5 answers

How to stream a JSON object to a HttpURLConnection POST request

I can not see what is wrong with this code: JSONObject msg; //passed in as a parameter to this method HttpURLConnection httpCon = (HttpURLConnection)…
AgilePro
  • 5,588
  • 4
  • 33
  • 56
16
votes
1 answer

HttpURLConnection is throwing exception

Here is my code to connect HTTP. URL url = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); String responseMsg =…
ray
  • 4,210
  • 8
  • 35
  • 46