3
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $urls[$vidCount]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);

So I have a cURL request that takes a URL and produces a file in the filesystem.

How do I emulate the above PHP cURL request in Java?

I have tried HttpURLConnection, but I am getting 403 Forbidden. The same call in cURL works properly. Is there some architecture difference between the two that I need to reconcile?

I believe it could be something in the headers that cURL might be setting automatically where Java is not. I'm not really sure but I would appreciate any advice I can get.

Thanks.

Marko
  • 20,385
  • 13
  • 48
  • 64
peetss
  • 91
  • 2
  • 5
  • 1
    http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java – Jasper de Vries Sep 26 '12 at 14:47
  • 1
    Take a look at this question: http://stackoverflow.com/questions/2586975/how-to-use-curl-in-java – rizidoro Sep 26 '12 at 15:10
  • curl has lots of hidden features you don't see, replicating every exact query might take a few twitches and I don't think you'll find a rosetta like thing to transform, I recommend just reading the libcurl documentation in php and seeing what every option means and then see how to implement that in your java library – fd8s0 Nov 14 '12 at 23:34

1 Answers1

1

I am not sure why you trying to emulate PHP behavior in Java. If the end result is really what you need to reach, than use the Java HttpURLConnection or even URLConnection.

asiby
  • 3,229
  • 29
  • 32