如何在Java中获取Web资源的修改日期?
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
// What now?
Run Code Online (Sandbox Code Playgroud) 我正在实施单点登录功能,以使用摘要式身份验证自动登录到附属的https网站.目前我的代码是
URL url = new URL(protocol, ip, port, path);
URLConnection connection = url.openConnection(Proxy.NO_PROXY);
connection.connect();
if (connection != null && connection.getHeaderFields() != null) {
if (connection.getHeaderFields().get(AUTHENTICATE_RESPONSE_HEADER) != null) {
Map<String, String> authenticateParameters = identifyAuthentication(connection);
String ha1 = calculateMD5(username + ":" + authenticateParameters.get("realm") + ":" + password);
String ha2 = calculateMD5("GET" + ":" + path);
String response = calculateMD5(ha1 + ":" +
authenticateParameters.get("nonce") + ":" +
"00000001" + ":" +
authenticateParameters.get("qop") + ":" +
ha2);
String authorizationRequest = authenticateParameters.get("challenge") + " " …
Run Code Online (Sandbox Code Playgroud) 我正在尝试阅读URL的内容.BufferedReader的初始化会导致崩溃.
我试过查找人们的解决方案,但无论我做什么,我都会遇到这个错误.
这是我的代码:
try {
String sUrl = "http://www.google.com";
System.out.println("About to create URL.");
URL url = new URL(sUrl);
System.out.println("Created URL");
System.out.println("About to create URLConnection");
URLConnection urlc = url.openConnection();
System.out.println("Created URLConnection");
System.out.println("About to create the BufferedReader");
BufferedReader bR = new BufferedReader(new
InputStreamReader(urlc.getInputStream()));
System.out.println("Created the BufferedReader");
System.out.println("About to create a StringBuilder");
StringBuilder sBuilder = new StringBuilder();
System.out.println("Created StringBuilder");
int byteRead;
System.out.println("About to enter while loop and build string");
while ((byteRead = bR.read()) != -1)
{
sBuilder.append((char) byteRead);
}
System.out.println("Built string");
bR.close();
System.out.println("Buffered …
Run Code Online (Sandbox Code Playgroud) 我有一个问题。当我尝试这样做时
connection.setRequestProperty("Sample", "Sample data \n");
Run Code Online (Sandbox Code Playgroud)
我得到java.lang.IllegalArgumentException:为什么我需要这样做。我的朋友Coz给我挑战,将其添加到服务器。我很困惑。请帮助我是否可以向URLConnection属性添加转义字符。
好奇而已。类 URLConnection 需要有两个不同的超时是否有充分的理由?
该connectTimeout
是在毫秒的最长等待时间,同时连接。如果在建立连接之前超时已过,则连接到服务器将失败并显示 SocketTimeoutException。
这readTimeout
是在放弃之前等待输入流读取完成的最长时间。如果在数据可用之前超时已过,则读取将失败并显示 SocketTimeoutException。
你能给我一个很好的理由为什么这两个值应该不同吗?为什么调用需要更多时间来执行连接而不是接收一些数据(反之亦然)?
我问这个是因为我必须配置这些值,我的想法是为两者设置相同的值。
我正在使用以下Java代码从Internet下载文件:
String address = "http://melody.syr.edu/pzhang/publications/AMCIS99_vonDran_Zhang.pdf";
URL url = new URL(address);
System.out.println("Opening connection to " + address + "...");
URLConnection urlC = url.openConnection();
urlC.setRequestProperty("User-Agent", "");
urlC.connect();
InputStream is = urlC.getInputStream();
FileOutputStream fos = null;
fos = new FileOutputStream("myFileName");
int oneChar, count = 0;
while ((oneChar = is.read()) != -1) {
System.out.print((char)oneChar);
fos.write(oneChar);
count++;
}
is.close();
fos.close();
System.out.println(count + " byte(s) copied");
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以下载文件的一部分。例如,对于一个5MB的文件,下载最后2MB。
我正在尝试从Android应用程序中的Web URL保存图像,但是当我运行它时,日志cat会抛出一个异常,说它是"只读".我不知道最近发生了什么.
这是我的下载类:
public class ImageDownload {
public static void downloader(String imageURL, String fileName) {
try {
URL url = new URL("http://www.exampleurl.com/" + imageURL);
File file = new File(fileName);
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.d("Downloader", "Error: " + e);
}
} …
Run Code Online (Sandbox Code Playgroud) android urlconnection filenotfoundexception fileoutputstream
我想从a读取前x个字节java.net.URLConnection
(虽然我不是被迫使用这个类 - 欢迎其他建议).
我的代码看起来像这样:
val head = new Array[Byte](2000)
new BufferedInputStream(connection.getInputStream).read(head)
IOUtils.toString(new ByteArrayInputStream(head), charset)
Run Code Online (Sandbox Code Playgroud)
它工作,但这个代码只加载网络的前2000个字节?
下一次试验
由于'JB Nizet'说使用缓冲输入流是没用的,所以我试了一下InputStreamReader
:
val head = new Array[Char](2000)
new InputStreamReader(connection.getInputStream, charset).read(head)
new String(head)
Run Code Online (Sandbox Code Playgroud)
此代码可能更好,但加载时间大致相同.那么这个程序是否限制了传输的字节?
我在Android项目中使用以下代码下载文件:
URL url = new URL(fileUrl);
URLConnection conection= url.openConnection();
conection.setDoOutput(true);
conection.connect();
int lenghtOfFile = conection.getContentLength();
Run Code Online (Sandbox Code Playgroud)
如果fileUrl是apk,则lenghtOfFile始终返回-1.
但如果是图像,视频类型,... lenghtOfFile返回正好.
为什么?
我正在使用eclipse,Android SDK修订版23.
当我想获取特定网页的源代码时,我使用以下代码:
URL url = new URL("https://google.de");
URLConnection urlConnect = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnect.getInputStream())); //Here is the error with the amazon url
StringBuffer sb = new StringBuffer();
String line, htmlData;
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
htmlData = sb.toString();
Run Code Online (Sandbox Code Playgroud)
上面的代码没有问题,但是当你的网址被调用时......
URL url = new URL("https://amazon.de");
Run Code Online (Sandbox Code Playgroud)
...然后你可能会得到一个IOException错误 - >服务器错误代码503.在我看来,这没有任何意义,因为我可以使用浏览器进入亚马逊网页而没有任何错误.
urlconnection ×10
java ×8
android ×3
url ×2
date ×1
download ×1
downloadfile ×1
scala ×1
timeout ×1