Onko olemassa joku valmis luokka, joka palauttaa URL-osoitteessa olevan sivun HEADERin?
Esim. Annetaan URL=http://keskustelu.suomi24.fi/ ja se palauttaisi "Suomi24 Keskustelu"
Esim. Annetaan URL=http://keskustelu.suomi24.fi/ ja se palauttaisi "Suomi24 Keskustelu"
Vastaukset 2
Vanhimmat
Siis -tagin sisällön? No juu, eipä taida olla sunin paketeissa valmista luokkaa tuohon.
Esimerkki napattu sivuilta
http://www.javaalmanac.com/
mene tuohon osoitteeseen ja anna search kenttään hakusanaksi url
Home > List of Packages > java.net [27 examples] > HTTP [4 examples]
e140. Getting the Response Headers from an HTTP Connection
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// List all the response headers from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i ) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
// No more headers
break;
}
if (headerName == null) {
// The header value contains the server's HTTP version
}
}
} catch (Exception e) {
}
Here's a sample of headers from a website:
Key=Value
null=HTTP/1.1 200 OK
Server=Netscape-Enterprise/4.1
Date=Mon, 11 Feb 2002 09:23:26 GMT
Cache-control=public
Content-type=text/html
Etag="9fa67d2a-58-71-3bbdad3283"
Last-modified=Fri, 05 Oct 2001 12:53:06 GMT
Content-length=115
Accept-ranges=bytes
Connection=close