urlconnection

connection.setRequestProperty and excplicitly writing to the urloutputstream are they same ?

URL url = new URL("http://www.example.com/comment"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); Is connection.setRequestProperty(key, value); the same as OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())...

How to navigate with URLConnection?

Hello, My application needs some web scraping functionality. I have URL object that downloads all the data. But I need to scrape many pages and I create many URL objects so I open many connections. How to optimize it, so I can have one connection and only navigate to other pages with it? Cheers ...

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

This subject is pretty often asked here and the Sun tutorial is too concise about the subject. So I thought, let's post a CW question and answer about this so that it can if necessary be referenced in other topics. Others are of course free to add more hints and best practices here. ...

How can I load txt file from internet into my jsf app?

Hi all! It's me again) I have another problem. I want to load file (for example - txt) from web. I tried to use the next code in my managed bean: public void run() { try { URL url = new URL(this.filename); URLConnection connection = url.openConnection(); bufferedReader = new BufferedReader(new InputStreamReader(connecti...

Java: Handling cookies when logging in with POST

I'm having quite some trouble logging in to any site in Java. I'm using the default URLconnection POST request, but I'm unsure how to handle the cookies properly. I tried this guide: http://www.hccp.org/java-net-cookie-how-to.html But couldn't get it working. I've been trying basically for days now, and I really need help if anyone wants...

Processing a website by using POST data and cookies

I try to access an ASPX-website where subsequent pages are returned based on post data. Unfortunately all my attempts to get the following pages fail. Hopefully, someone here has an idea where to find the error! In step one I read the session ID from the cookie as well as the value of the viewstate variable in the returned html page. St...

Get modified date of web resource in Java

How do you get the modified date of a web resource in Java? URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.connect(); // What now? ...

Help with java.net.URLConnection timeout?

A Connection is timing out, and the developer on it is at the bottom of his list of ideas. The logs have a friendly: [6/24/10 6:32:34:032 EDT] 0000000d ThreadMonitor W WSVR0605W: Thread "WebContainer : 136" (0000c53e) has been active for 719542 milliseconds and may be hung. There is/are 45 thread(s) in total in the server that may b...

Java URLConnection Timeout

Hi all, I am trying to parse an XML file from an HTTP URL. I want to configure a timeout of 15 seconds if the XML fetch takes longer than that, I want to report a timeout. For some reason, the setConnectTimeout and setReadTimeout do not work. Here's the code: URL url = new URL("http://www.myurl.com/sample.xml"); URL...

Java URLConnection

Simple stuff, I am learning URLs/Networking in my class and I am trying to display something on a webpage. Later I am going to connect it to a MySQL DB... anyway here is my program: import java.net.*; import java.io.*; public class asp { public static URLConnection connection; public static void main(String[] args) { ...

Reading binary file from URLConnection

I'm trying to read a binary file from a URLConnection. When I test it with a text file it seems to work fine but for binary files it doesn't. I'm using the following mime-type on the server when the file is send out: application/octet-stream But so far nothing seems to work. This is the code that I use to receive the file: file = Fil...

Getting HTTP 400 on randome times on hitting url to get Twitter followers/friends - java

I am developing an application on Twitter. And Twitter is giving me response of 400 on random times. I am in panic. Their servers are too bad to entertain requests. I am hitting their REST URL to get followers with cursor. Some times I get first 100 followers and on second cursor iteration I got HTTP 400. Only one attempt gave me my 3...

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

Is there any convenient way to read and parse data from incoming request. E.g client initiate post request URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); PrintWriter writer = null; try { OutputStream ...

Java Load image from URLConnection

Hello. I have a URLConnection, that access a web page. URL url = new URL("https://domain"); con = url.openConnection(); con.setDoOutput(true); Then i sent some data to the server using con.setRequestProperty() I get the response cookies fro ma specified field using String headerValue = con.getHeaderField(6); I also get the...

Java: resume Download in URLConnection

Hi I wrote a program that downloads some files from some servers. Currently program works properly. But I want to add resume support to it. I'm doing it like this But the result file is corrupted: .... File fcheck=new File(SaveDir+"/"+filename); if(resumebox.isSelected() && fcheck.exists()){ connection.setRequestProperty("Range",...

java.io.IOException: Server returned HTTP response code: 500

I'm facing this problem with Java. I want to get some HTML informations from a URL. This code was working for so long, but suddenly, it stopped working. When I access this URL using the browser, it opens with no problem. The code: URL site = new URL(this.url); java.net.URLConnection yc = site.openConnection(); BufferedReader in = new ...

How do I do an FTP delete with Java URLConnection?

I have a simple put and get working, but can't seem to find how to do a delete? For reference, the put code is: BufferedInputStream inStream = null; FileOutputStream outStream = null; try { final String ftpConnectInfo = "ftp://"+user+":"+pass+"@"+destHost+"/"+destFilename+";type=i"; LOGGER.info("Connection String: {}", ftpCon...

Calling setConnectTimeout() for URLConnection doesn't have an affect?

I am making a URLConnection to my companies server, and now that we have moved to the production server, I am receiving connection timeouts. It is known that the server is slow, so in order to accommodate it, I was asked to extend the amount of time before the connection times out on my device. I checked my code, and saw that I had alre...

can't get response header location using JAVA's URLConnection

Hi all, can someone kindly suggest what I'm doing wrong here? I'm trying to get the header location for a certain URL using java here is my code: URLConnection conn = url.openConnection(); String location = conn.getHeaderField("Location"); it's strange since I know for sure the URL i'm refering to return a Location header and using ...

How to retrieve a JSON object from HttpServletResponse in Spring?

I have a Spring controller in which I have to read a JSON object from a URLConnection. Currently I am doing this by reading from the connection's input stream line by line. Basically I am reading the text contents of the response line by line. Is there any JSON api that I could use to populate the JSON object directly from the URLConnec...