-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!

10
  • What you should have found in your researches is that 403 means Forbidden. You are not authorized to access this resource. Commented Mar 31, 2022 at 1: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. Commented Mar 31, 2022 at 1: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. Commented Mar 31, 2022 at 1: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. Commented Mar 31, 2022 at 1:31
  • 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. Commented Mar 31, 2022 at 2:40

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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!

4 Comments

what's the point of duplicating exsisting answer? 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.
@LeiYang It is not a duplicate of that answer. It uses a different technique to set the header.
his link contain your way http.agent.
@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..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.