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
24
votes
5 answers

java.io.IOException : No authentication challenges found

I am newbie to android and this is my first project on android. I am struggling with "authentication" problem for more than a day. I tried several options but none of them worked. Basically, I want to call a REST API and get response. I am sure…
Geek
  • 8,280
  • 17
  • 73
  • 137
23
votes
1 answer

HttpURLConnection timeout defaults

I seem to be having trouble with some tcp requests getting "stuck" at times, like it is waiting for some response but the connection has been "severed" so a response will never come. Is this expected behavior for HttpURLConnection with default…
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
23
votes
4 answers

Create time out scenario in SOAPUI

I am working on a web-project. I have created one Http Url Connection. But for that, I have to test the code for time-out InterruptedIOException, that will execute on time-out, but even after setting time-out time as 1msec, my case is executed…
pankaj_ar
  • 757
  • 2
  • 10
  • 33
22
votes
2 answers

How to read full response from HttpURLConnection?

I make some proxy server in andorid which modify http headers, it works ok, but I have to forward full response to 'top layer'. How I can read whole response (all headers, content, everything) from HttpURLConnection? HttpURLConnection…
Andrzej
  • 304
  • 1
  • 2
  • 13
22
votes
2 answers

How to set the (OAuth token) Authorization Header on an Android OKHTTPClient request

I'm able to set the Auth Header on normal HTTPURLConnection requests like this: URL url = new URL(source); HttpURLConnection connection = this.client.open(url); connection.setRequestMethod("GET"); connection.setRequestProperty("Authorization",…
Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74
21
votes
0 answers

HttpURLConnection buffering rather than streaming, despite setting chunked streaming mode?

I am using HttpURLConnection to write files, some of which are quite large, to a server. final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); A while back I had issues writing objects 1 GB or more. I fixed that by setting it to…
21
votes
3 answers

Persistent HttpURLConnection in Java

I am trying to write a java program that will automatically download and name some of my favorite web comics. Since I will be requesting multiple objects from the same domain, I wanted to have a persistent http connection that I could keep open…
Githaron
  • 213
  • 1
  • 2
  • 6
20
votes
3 answers

Sending UTF-8 string using HttpURLConnection

till now I've used the following code snippet in order to send and recieve JSON strings: static private String sendJson(String json,String url){ HttpClient httpClient = new DefaultHttpClient(); String responseString = ""; try { …
Jjang
  • 11,250
  • 11
  • 51
  • 87
20
votes
3 answers

Get body of Bad Request httpURLConnection.getInputStream()

I've been working on a portlet that calls Rest API. When the API is called and the requested data doesn't exist, it returns an appropriate error message in JSON format (with Bad request http code - 400), and if the id exists, it returns the…
Rez
  • 470
  • 2
  • 8
  • 14
19
votes
2 answers

cURL and HttpURLConnection - Post JSON Data

How to post JSON data using HttpURLConnection? I am trying this: HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection())); httpcon.setDoOutput(true); httpcon.setRequestProperty("Content-Type",…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
19
votes
4 answers

Tomcat, HTTP Keep-Alive and Java's HttpsUrlConnection

I have two Tomcat servers that need to maintain a persistent connection to cut down on SSL handshaking. One server (the proxy) sits in a DMZ while the other one is safely behind another firewall. The proxy basically just runs a simple servlet that…
James Sheppard
  • 301
  • 1
  • 4
  • 15
19
votes
3 answers

Difference between okhttp and httpurlconnection?

What are the differences between these 2 libraries? How I understood there is a difference between these 2 lib also because Volley uses httpurlconnection and Retrofit the okhttp.... But I don't understand the difference between them and pros and…
user155293
  • 373
  • 1
  • 3
  • 14
19
votes
4 answers

HttpUrlConnection setting Range in Android is ignored

I'm trying get a 206 response from my server using Android. Here's the code. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); new AsyncTask
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
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?
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
19
votes
4 answers

getRequestProperty("Authorization") always returns null

I am trying to read the authorization header for an HTTP request (because I need to add something to it), but I always get null for the header value. Other headers work fine. public void testAuth() throws MalformedURLException, IOException{ …
Thilo
  • 257,207
  • 101
  • 511
  • 656