How to download file despite of 401 error in Java [closed]
I would like to download a file from the Internet using Java, but from time to time my program shows 401 error and I cannot get it.
What is interesting when I put link to this file on HTML website, I can download it by clicking right click and "Save as".
What can I do to achieve the same effect in Ja开发者_如何学Gova ?
Check what the browser actually sends, especially the headers, and make sure you do the same. Cookies, authenticated and referre fields may be interesting.
As you got an unauthorized it may be necessary to go to the website first with java and ensure you get your session cookie (if applicable).
It could even be that access to the html page makes the server allow downloads for some time, or that the number of dowloads per time unit is limited. In the last case there's nothing you can do.
From Wikipedia:
401 Unauthorized
Similar to 403 Forbidden, but specifically for use when authentication is possible but has failed or not yet been provided.[2] The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.
Apparently there is some authentication-related issue that your Java code does not deal with - or some server-related issue that the browser does deal with. For us to be able to help you more you need to:
Provide us with the relevant parts of your code.
Provide us with information on your link. If the link does not refer to simple static content there are about a million different things that could go wrong.
Some things to look for:
Use a network sniffer (e.g. Wireshark) to find out what is different between the Java and browser sessions.
Print out all headers received and sent by the Java code, and compare to what the network sniffer or Live Http Headers shows.
Is your code handling any authentication cookies correctly?
Does your Java code work fine if you have already accessed the link from a browser?
精彩评论