0

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:

  1. What's this Python code equivalence for Java ?

  2. Also, is it possible to make an asynchronous download with a callback when the download is completed ?

Noor
  • 19,638
  • 38
  • 136
  • 254

1 Answers1

1

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

Community
  • 1
  • 1
Thrasd
  • 138
  • 1
  • 6