Calling a Servlet from a Java application

URLConnection is only lazily executed whenever you call any of the get methods. Add the following to your code to actually execute the HTTP request and obtain the servlet response body. InputStream response = servletConnection.getInputStream(); See also: How to use java.net.URLConnection to fire and handle HTTP requests?

How can I specify the local address on a java.net.URLConnection?

This should do the trick: URL url = new URL(yourUrlHere); Proxy proxy = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress( InetAddress.getByAddress( new byte[]{your, ip, interface, here}), yourTcpPortHere)); URLConnection conn = url.openConnection(proxy); And you are done. Dont forget to handle exceptions nicely and off course change the values to suit your scenario. Ah and I omitted the import statements

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

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

URLConnection FileNotFoundException for non-standard HTTP port sources

The response to my HTTP request returned with a status code 404, which resulted in a FileNotFoundException when I called getInputStream(). I still wanted to read the response body, so I had to use a different method: HttpURLConnection#getErrorStream(). Here’s a JavaDoc snippet of getErrorStream(): Returns the error stream if the connection failed but the server … 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