Do to download a file and set its content type using Java, using python I do it this way:
import urllib2
req = urllib2.Request('http://www.example.com/', data="abc", headers={'Content-type': 'text/plain'})
r = urllib2.urlopen(req)
My questions are:
What's this Python code equivalence for Java ?
Also, is it possible to make an asynchronous download with a callback when the download is completed ?
I am unable to make a comment, so it has to go here. Have you tried any of the many suggestions from how-to-download-and-save-a-file-from-internet-using-java
Update: ok, so you should be able to use a HttpURlConnection
URL url = new URL("http://www.example.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Set Headers
conn.setRequestProperty("Content-Type", "text/plain");
and then you can use conn.getInputStream()
to read the response
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments