Java: how to use UrlConnection to post request with authorization?

A fine example found here. Powerlord got it right, below, for POST you need HttpURLConnection, instead. Below is the code to do that, URL url = new URL(urlString); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setRequestProperty (“Authorization”, encodedCredentials); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(data); writer.flush(); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) … Read more

Getting “java.net.ProtocolException: Server redirected too many times” Error

It’s apparently redirecting in an infinite loop because you don’t maintain the user session. The session is usually backed by a cookie. You need to create a CookieManager before you use URLConnection. // First set the default cookie manager. CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); // All the following subsequent URLConnections will use the same cookie manager. URLConnection … Read more

Apache HTTP client or URLConnection [duplicate]

Google has silently deprecated Apache HTTP client usage since Gingerbread: http://android-developers.blogspot.com/2011/09/androids-http-clients.html. And while they didn’t mark it with deprecated annotation, they suggest you to use HttpURLConnection for new applications as: it is where we [Google] will be spending our energy going forward. Personally I don’t like that decision and would rather stick to HttpClient 4.1+, … Read more

Java URLConnection Timeout

Try this: import java.net.HttpURLConnection; URL url = new URL(“http://www.myurl.com/sample.xml”); HttpURLConnection huc = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(false); huc.setConnectTimeout(15 * 1000); huc.setRequestMethod(“GET”); huc.setRequestProperty(“User-Agent”, “Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)”); huc.connect(); InputStream input = huc.getInputStream(); OR import org.jsoup.nodes.Document; Document doc = null; try { doc = Jsoup.connect(“http://www.myurl.com/sample.xml”).get(); } catch (Exception e) { … Read more

Convenient way to parse incoming multipart/form-data parameters in a Servlet [duplicate]

multipart/form-data encoded requests are indeed not by default supported by the Servlet API prior to version 3.0. The Servlet API parses the parameters by default using application/x-www-form-urlencoded encoding. When using a different encoding, the request.getParameter() calls will all return null. When you’re already on Servlet 3.0 (Glassfish 3, Tomcat 7, etc), then you can use … Read more

Upload files from Java client to a HTTP server

You’d normally use java.net.URLConnection to fire HTTP requests. You’d also normally use multipart/form-data encoding for mixed POST content (binary and character data). Click the link, it contains information and an example how to compose a multipart/form-data request body. The specification is in more detail described in RFC2388. Here’s a kickoff example: String url = “http://example.com/upload”; … Read more

Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?

Connection timeouts (assuming a local network and several client machines) typically result from a) some kind of firewall on the way that simply eats the packets without telling the sender things like “No Route to host” b) packet loss due to wrong network configuration or line overload c) too many requests overloading the server d) … Read more