我的android应用程序可以从urls下载具有特定文件模式的文件(比如*.pdf)。
但是一些url需要用户授权才能下载文件。我不想显示这样的网址的授权屏幕。我只想传达给用户,这个文件不能通过此应用程序下载。
我如何在java/android中做到这一点?
void download(String url)
{
URL url = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//TODO : stop download if detected that this url requires authorization/authentication.
urlConnection.disconnect();
}发布于 2016-05-16 15:17:27
请从响应中获取HTTP状态,如下所示。
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");//Or POST, depending on your service
connection.connect();
int code = connection.getResponseCode();如果您的web服务是Restful风格的服务,则应该返回401。
https://stackoverflow.com/questions/37248170
复制相似问题