urlconnection

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

I'm getting a ConnectException: Connection timed out with some frequency from my code. The URL I am trying to hit is up. The same code works for some users, but not others. It seems like once one user starts to get this exception they continue to get the exception. Here is the stack trace: java.net.ConnectException: Connection timed...

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

My Tomcat instance is listening to multiple IP adress, but I want to control what source IP address is used when opening a URLConnection. How can I specify this? ...

What is the proper way of setting headers in a URLConnection?

My code is like the following: URLConnection cnx = address.openConnection(); cnx.setAllowUserInteraction(false); cnx.setDoOutput(true); cnx.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); InputStream is = cnx.getInputStream(); Is it ok if I set the headers before I get the InputStr...

Java UrlConnection HTTP 1.0

I am trying to download a file from my Java application. But because UrlConnection uses HTTP 1.1 protocol i get a Tranfer Encoding: chunked response in which case i can not find out file size(content-length is not set). From what i could find HTTP version is hard coded in the class and there is no way to change it. Is it somehow possible...

Make URL Request from Servlet and retain Header information

I need to make a request from a servlet but I also need to retain all the header information. That is in the request object. For example, I if I do something like the following from the doGet method, is there a simple way to just pass that information to the URL Connection object? URL url = new URL(); URLConnection uc = url.openConnec...

Java URL Connection blank input stream

The application I am working on has a terribly slow loading web page. If I run the following code it works fine but only because of the call to sleep. If I don't sleep then the InputStream is just a bunch of spaces, probably due to the application it is calling from. Is there any non-hack way around this? public class PublishTool e...

How to send an HTTP header in java

Hi, Is it possible to send a Http header via a URL connection in java? I had this working using sockets, but ran into issues with a firewall which don't seem to be a problem with URLConnection. From looking at the API I get the impression that the output methods in URLConnection are just for filling in forms etc, or can they be used to s...

Calling servlet from another servlet

I have two servlets which are running on different tomcat servers. I and trying to call a servlet1 from servlet2 in the following way and wanted to write an object to output stream. URL url=new URL("http://msyserver/abc/servlet1"); URLConnection con=url.openConnection(); con.setDoOutput(true); con.setDoInput(true); OutputStream os=con...

URLConnection FileNotFoundException for non-standard HTTP port sources

I was trying to use the Apache Ant coretask Get to get a list of WSDLs generated by another team in our company. They have them hosted on a weblogic 9.x server on http://....com:7925/services/. I am able to get to the page through a browser, but the get task gives me a FileNotFoundException when trying to copy the page to a local file ...

In java, how to create HttpsURLConnection or HttpURLConnection based on the url?

I'm working on a project where I'm creating a class to run http client requests (my class acts as a client). It takes in a url and a request method (GET, POST, PUT, etc) and I want to be able to parse the URL and open a HttpsURLConnection or HttpURLConnection based on whether it is https or http (assume the given urls will always be cor...

Java URLConnection crashes the entire process when I call getInputStream

I am writing a Java applet that downloads images from a web server and displays them to the user. It works fine in Java 1.6.0_3 and later, but on older versions it will completely crash the process about once every 20 page views. There are no error messages in the Java console, because the process is completely frozen. I've waited for al...

Connection to a URL from within an applet using Apache's HttpClient vs using the JDK's URLConnection

In the following code, I have verified that connecting to a URL from within an applet preserves the browser's session if JDK's URLConnection class is used. However, this is not the case if Apache's HttpClient library is used. Does anyone know why? Alternatively, is there a way for me to set the connection instance to be used by an Htt...

Websphere slow URLConnection

We have a Websphere version 6.1 deployed on a Linux environment. To open an https connection we call openConnection() and connect() on a java.net.URL object. Opening this connection takes aproximately 3 minutes. Once the connection is established we can write and read data very fast. Using openssl a connectiontest (ssh console) works v...

How to optimally clean up outgoing UrlConnection objects in high use multithreaded environment?

I have a use case where a servlet (that has reasonably high concurrent use) makes an outgoing URLConnection to retrieve some data REST style as part of its normal server-side processing logic. The connection is created and used each time the servlet is invoked as the URL is potentially different (but the domain is always the same). I ...

How does URLConnection.setUseCaches() work in practice?

I have an Applet which is loading images over a http connection using URLConnection. I am setting setUseCaches(true) for all connections, but still not seeing any caching behavior. My image's HTTP headers have reasonable cache settings. If you look at bug 4528599 there is this rather mysterious statement: The current version (1.3.1) ...

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

Hi, I would lake to generate POST request to a server which requires authentication. I tried to use the following method: private synchronized String CreateNewProductPOST (String urlString, String encodedString, String title, String content, Double price, String tags) { String data = "product[title]=" + URLEncoder.encode(title) + ...

Why doesn't HttpClient work but HttpUrlConnenction do when posting data to form.

Hello experts! I'm building an android app which should perform a GET on my site to get two cookies and then perform a post to the same site with these cookies. As mentioned I start of with the GET and I'm using org.apache.http.client.HttpClient to perform this operation. String requiredCookies = ""; HttpContext localContext = null;...

On Android/Java, how many bytes has a connection downloaded?

An Android app I'm writing involves quite a lot of downloading of content (think podcatcher/RSS). I would like to be able to give the user an indication of how many bytes they've downloaded, so they can make the decision whether they want to use Wifi or not. To this end, I have found a way of counting the number of bytes read by the a...

Cannot add Authorization field to HttpsUrlConnection in order to complete Basic authentication

Hi, I'm using the Sun API HttpsURLConnection class, and have been trying for a day now to get it to send a simple request: URL url = new URL("https://thirdpartyserver.com/somelocation"); connection = (HttpsURLConnection)url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); if (doAuthorization) { ...

copying the request header from request object to urlConnection object

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { URL url = new URL("http://localhost:8080/testy/Out"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); Pr...