-1

Im trying to download a file, but for some people running it, the server is giving error 403.

try (BufferedInputStream in = new BufferedInputStream(new URL("http://example.com/test.zip").openStream());
FileOutputStream fileOutputStream = new FileOutputStream("./test.zip")) {
  byte dataBuffer[] = new byte[1024];
  int bytesRead;
  while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
     fileOutputStream.write(dataBuffer, 0, bytesRead);
  }
} catch (IOException e18) {
  error("Error: "+e18);
  e18.printStackTrace();
  return false;
}

While researching this error(403 - Forbidden), I found multiple posts saying that a user agent needs to be specified, I believe this may be the case, I am not sure how to easily add a user agent to my code.

Thank You in advance!

DevSteven
  • 21
  • 3
  • What you should have found in your researches is that 403 means Forbidden. You are not authorized to access this resource. – user207421 Mar 31 '22 at 01:23
  • @user207421 I'm aware of this, and I believe It's being blocked because of the user agent not being specified from what I can see. – DevSteven Mar 31 '22 at 01:26
  • Whatever you may see, all we can see is what you posted, which didn't include you finding Forbidden as the meaning of 403. You may or may not be right about the User-Agent: it depends on the server, as does the original permission problem. You might need a username password (supplied via `Authenticator`) instead. – user207421 Mar 31 '22 at 01:28
  • @user207421 I own and host the webserver that it's connecting to. It does not require Authentication in order to access the file, you can access the file just fine from the browser, and as I said in my post, multiple users have no problem with the above code, only some people do. – DevSteven Mar 31 '22 at 01:31
  • when i try to access the url `http://example.com/test.zip` in browser, it give me a page instead of zip file. – Lei Yang Mar 31 '22 at 01:35
  • @LeiYang Just put that as an example of what I'm doing, I didn't want to "expose" my webserver at all. – DevSteven Mar 31 '22 at 01:39
  • i doubt you own and host the site, at least you even don't know it has some restrictions to resource access. how do you host, use webserver like nginx for example, or some other code? – Lei Yang Mar 31 '22 at 01:40
  • @LeiYang I use Nginx, but yes, I own the machine, and I set up the Nginx server. – DevSteven Mar 31 '22 at 01:45
  • can you paste the nginx.conf file content? is it a simple static site, or dynamic one? – Lei Yang Mar 31 '22 at 01:47
  • 1
    So if you own the machine you already know what User-Agents are acceptable. So set one of those. Hard to see why you are asking here. – user207421 Mar 31 '22 at 02:40

2 Answers2

2
URL tgtUrl = new URL("http://example.com/test.zip");
java.net.URLConnection c = tgtUrl .openConnection();
c.setRequestProperty("User-Agent", " USER AGENT STRING HERE ");
ReadableByteChannel tar = Channels.newChannel(c.getInputStream());

OR

URL tgtUrl = new URL("http://example.com/test.zip");
java.net.URLConnection c = tgtUrl .openConnection();
c.setRequestProperty("User-Agent", " USER AGENT STRING HERE ");
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println(br.readLine());

Ref : Java: Download from an URL

Might be duplicate question

Senthil
  • 2,156
  • 1
  • 14
  • 19
0

Simply adding:

System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7");

Fixed it! Thanks everyone!

DevSteven
  • 21
  • 3
  • what's the point of duplicating [exsisting answer](https://stackoverflow.com/a/71685870/1518100)? and i still suspect your special website setting result to this issue, a normal website don't have to set agent to access static files. – Lei Yang Mar 31 '22 at 02:33
  • @LeiYang It is not a duplicate of that answer. It uses a different technique to set the header. – user207421 Mar 31 '22 at 02:39
  • his link contain your way [http.agent](https://stackoverflow.com/a/8884191/1518100). – Lei Yang Mar 31 '22 at 02:42
  • @LeiYang My answer is a different technique altogether and works with the code I provided in my question without re-writing it all... "a normal website don't have to set agent to access static files" For most websites, you do need a valid one... The issue I was having is it was sending it with either no agent or an invalid agent... A system normally has a default agent. Sometimes this agent is invalid.. – DevSteven Apr 01 '22 at 02:06