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
31
votes
6 answers

Why do you have to call URLConnection#getInputStream to be able to write out to URLConnection#getOutputStream?

I'm trying to write out to URLConnection#getOutputStream, however, no data is actually sent until I call URLConnection#getInputStream. Even if I set URLConnnection#doInput to false, it still will not send. Does anyone know why this is? There's…
John
  • 9,254
  • 12
  • 54
  • 75
31
votes
2 answers

Sending data via request header vs sending data via request body

What is the difference between sending data through the request header and sending data through the request body. Under what circumstances, we have to send the data through the header/body and when shouldn't we send the data through header/body ?
UnahD
  • 867
  • 2
  • 10
  • 25
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
30
votes
9 answers

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found

I have some typical codes which used HttpURLConnection to get a file with an URL. They worked fine in android 1.x and 2.x. But failed in Android 4.1! I searched on the web but found little similar information. Would anybody please help to…
firebear
  • 774
  • 1
  • 8
  • 19
29
votes
2 answers

How to send Request payload to REST API in java?

I want to retrieve the JSON data from the following: https://git.eclipse.org/r/#/c/11376/ Request URL: https://git.eclipse.org/r/gerrit/rpc/ChangeDetailService Request Method: POST Request…
Gangaraju
  • 4,406
  • 9
  • 45
  • 77
29
votes
1 answer

Should HttpURLConnection with CookieManager automatically handle session cookies?

I have a Java application (JDK 1.7.0_13) and am using java.net.HttpURLConnection to connect to some servlet based services that do session management. I am trying to figure out how to use java.net.CookieManager to track session cookies. Reading…
Chuck
  • 1,850
  • 2
  • 17
  • 28
29
votes
5 answers

HttpURLConnection.getResponseCode() returns -1 on second invocation

I seem to be running into a peculiar problem on Android 1.5 when a library I'm using (signpost 1.1-SNAPSHOT), makes two consecutive connections to a remote server. The second connection always fails with a HttpURLConnection.getResponseCode() of…
emmby
  • 99,783
  • 65
  • 191
  • 249
28
votes
4 answers

How do I persist cookies when using HTTPUrlConnection?

I've begun using the recommended HTTPUrlConnection and moved away from the DefaultHTTPClient. One of the things that I haven't been able to glue back together is the use of a persistent cookie store. I'd like to simply attach a custom cookie…
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
27
votes
1 answer

Using HttpURLConnection and HttpsURLConnection to connect to an https?

What are the differences if I try to connect to an "https" using HttpURLConnection and HttpsURLConnection? I was able to connect to "https" using both HttpURLConnection and HttpsURLConnection so I am confused. I thought I can only establish a…
Arci
  • 6,647
  • 20
  • 70
  • 98
27
votes
3 answers

Java Webstart and URLConnection caching API

The description of the URLConnection caching API states as the last sentence: There is no default implementation of URLConnection caching in the Java 2 Standard Edition. However, Java Plugin and Java WebStart do provide one out of the box. Where…
26
votes
3 answers

Upload large file in Android without outofmemory error

My upload code as below: String end = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { URL url = new URL(ActionUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); …
brian
  • 6,802
  • 29
  • 83
  • 124
26
votes
5 answers

Errorstream in HttpUrlConnection

I want to do a POST request to an HTTP Servlet I wrote myself. Good case (HTTP response Code 200) always works fine by using URL.openConnection() method. But when I receive a desired error response code (e.g. 400) then I thought I have to use…
Matthias Herbst
  • 261
  • 1
  • 3
  • 3
26
votes
3 answers

How to modify the header of a HttpUrlConnection

Im trying to improve the Java Html Document a little but i'm running into problems with the HttpUrlConntion. One thing is that some servers block a request if the user agent is a Java VM. Another problem is that the HttpUrlConnection does not set…
tigger
  • 1,856
  • 3
  • 18
  • 23
26
votes
2 answers

How do you send data in a Request body using HttpURLConnection?

I am using HttpURLConnection to make a POST request to a local service deployed locally and created using JAVA Spark. I want to send some data in request body when I make the POST call using the HttpURLConnection but every time the request body in…
utkarsh31
  • 1,439
  • 2
  • 13
  • 20
24
votes
5 answers

Java: Display request of an HttpURLConnection before sending

I want to make some API calls to a server using HttpURLConnection. But the requests are not successful, returning: 400 Bad Request Unexpected request Content-Type header ''. Expecting…
Hirnhamster
  • 7,101
  • 8
  • 43
  • 73