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.

3175 questions
2006
votes
11 answers

How to use java.net.URLConnection to fire and handle HTTP requests?

Use of java.net.URLConnection is asked about pretty often here, and the Oracle tutorial is too concise about it. That tutorial basically only shows how to fire a GET request and read the response. It doesn't explain anywhere how to use it to among…
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
552
votes
39 answers

"PKIX path building failed" and "unable to find valid certification path to requested target"

I'm trying to get tweets using twitter4j library for my java project which uses under the covers java.net.HttpURLConnection (as can be seen in stack trace). On my first run I got an error about certificate sun.security.validator.ValidatorException…
bofanda
  • 7,206
  • 7
  • 31
  • 55
334
votes
18 answers

Java - sending HTTP parameters via POST method easily

I am successfully using this code to send HTTP requests with some parameters via GET method void sendRequest(String request) { // i.e.: request = "http://example.com/index.php?param1=a¶m2=b¶m3=c"; URL url = new URL(request); …
dan
  • 15,066
  • 19
  • 57
  • 88
269
votes
16 answers

How to add parameters to HttpURLConnection using POST using NameValuePair

I am trying to do POST with HttpURLConnection(I need to use it this way, can't use HttpPost) and I'd like to add parameters to that connection such as post.setEntity(new UrlEncodedFormEntity(nvp)); where nvp = new…
Michal
  • 14,455
  • 9
  • 68
  • 97
146
votes
4 answers

How do I do a HTTP GET in Java?

How do I do a HTTP GET in Java?
David
139
votes
5 answers

Can you explain the HttpURLConnection connection process?

I am using HTTPURLConnection to connect to a web service. I know how to use HTTPURLConnection but I want to understand how it works. Basically, I want to know the following: On which point does HTTPURLConnection try to establish a connection to the…
Arci
  • 6,327
  • 20
  • 65
  • 95
136
votes
9 answers

How to send PUT, DELETE HTTP request in HttpURLConnection?

I want to know if it is possible to send PUT, DELETE request (practically) through java.net.HttpURLConnection to HTTP-based URL. I have read so many articles describing that how to send GET, POST, TRACE, OPTIONS requests but I still haven't found…
Matrix
  • 7,431
  • 13
  • 61
  • 93
133
votes
12 answers

Connecting to remote URL which requires authentication using Java

How do I connect to a remote URL in Java which requires authentication. I'm trying to find a way to modify the following code to be able to programatically provide a username/password so it doesn't throw a 401. URL url = new…
user14128
  • 2,419
  • 4
  • 19
  • 16
125
votes
10 answers

Sending files using POST with HttpURLConnection

Since the Android developers recommend to use the HttpURLConnection class, I was wondering if anyone can provide me with a good example on how to send a bitmap "file" (actually an in-memory stream) via POST to an Apache HTTP server. I'm not…
Mihai Todor
  • 7,465
  • 9
  • 44
  • 77
113
votes
8 answers

FileNotFoundException while getting the InputStream object from HttpURLConnection

I am trying to send a post request to a url using HttpURLConnection (for using cUrl in java). The content of the request is xml and at the end point, the application processes the xml and stores a record to the database and then sends back a…
naiquevin
  • 6,868
  • 12
  • 49
  • 62
107
votes
3 answers

How to get response body using HttpURLConnection, when code other than 2xx is returned?

I have problem with retrieving Json response in case when server returns error. See details below. How I perform the request I use java.net.HttpURLConnection. I setup request properties, then I do: conn = (HttpURLConnection)…
kiedysktos
  • 3,201
  • 5
  • 23
  • 36
104
votes
5 answers

POST request send JSON data Java HttpUrlConnection

I have developed a Java code that convert the following cURL to java code using URL and HttpUrlConnection. the cURL is : curl -i 'http://url.com' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": {…
user3244172
  • 1,072
  • 2
  • 8
  • 8
103
votes
6 answers

HTTPURLConnection Doesn't Follow Redirect from HTTP to HTTPS

I can't understand why Java's HttpURLConnection does not follow an HTTP redirect from an HTTP to an HTTPS URL. I use the following code to get the page at https://httpstat.us/: import java.net.URL; import java.net.HttpURLConnection; import…
Shcheklein
  • 5,090
  • 7
  • 37
  • 48
98
votes
8 answers

Read error response body in Java

In Java, this code throws an exception when the HTTP result is 404 range: URL url = new URL("http://stackoverflow.com/asdf404notfound"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.getInputStream(); // throws! In my…
Dan Fabulich
  • 31,628
  • 35
  • 121
  • 153
86
votes
16 answers

HttpURLConnection Invalid HTTP method: PATCH

When I try to use a non-standard HTTP Method like PATCH with URLConnection: HttpURLConnection conn = (HttpURLConnection) new URL("http://example.com").openConnection(); conn.setRequestMethod("PATCH"); I get an…
kavai77
  • 5,384
  • 6
  • 30
  • 46
1
2 3
99 100