Reading a page header with getHeaderField

Status
Nicht offen für weitere Antworten.

cd-rw

Mitglied
How can I read the file header of a page using getHeaderField()? I'm new to java and I guess this is a pretty easy thing to do. What I want to do is to read the header "tems" but exactly how should I write my program? Any help would be appreciated.
 

P3AC3MAK3R

Top Contributor
Hi,

maybe the following code snippet will help you out:

Code:
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) {
}

Source: http://javaalmanac.com/egs/java.net/GetHeaders.html
 

cd-rw

Mitglied
P3AC3MAK3R hat gesagt.:
Hi,

maybe the following code snippet will help you out:

Code:
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) {
}

Source: http://javaalmanac.com/egs/java.net/GetHeaders.html

Ok, thanks for the code. If I wanted to make that a stand alone program how would I go about it? I do have a compilator.
 

cd-rw

Mitglied
P3AC3MAK3R hat gesagt.:
Just create a new class like in the following example:

Code:
public class HeaderTest {
    public static void main(String[] args) {

        // add the code from above here

    }
}

btw: it's "compiler" and not "compilator". ;)

Checkout the following tutorial for an introduction to the Java basics:

http://java.sun.com/docs/books/tutorial/reallybigindex.html

Ok, thanks for the help. Unfortunately I'm getting three error messages:

Code:
/tmp/21194/HeaderTest.java:6: cannot resolve symbol
symbol  : class URL 
location: class HeaderTest
    URL url = new URL("http://www.tes.com");
    ^
/tmp/21194/HeaderTest.java:6: cannot resolve symbol
symbol  : class URL 
location: class HeaderTest
    URL url = new URL("http://www.tes.com");
                  ^
/tmp/21194/HeaderTest.java:7: cannot resolve symbol
symbol  : class URLConnection 
location: class HeaderTest
    URLConnection conn = url.openConnection();
    ^
3 errors

That's when I'm using this code:

Code:
public class HeaderTest {
    public static void main(String[] args) {

        try {
    // Create a URLConnection object for a URL
    URL url = new URL("http://www.mod-x.co.uk/mod_x_LeV_2/M_LeVeL3_od/mod_x_3.php");
    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) {
}

    }
}

Roar hat gesagt.:
there is also an english forum on http://www.java-forum.org

Oops, I missed that.
 
R

Roar

Gast
the copmiler can't found these class because you have not imported it, but it's neccessary:
Code:
import java.net.URL;
import java.net.URLConnection;
 

cd-rw

Mitglied
Roar hat gesagt.:
the copmiler can't found these class because you have not imported it, but it's neccessary:
Code:
import java.net.URL;
import java.net.URLConnection;

Ok, that was an easy mistake. Anyway I managed to compile the .java file to a .class file but where do I go from here? Should I put it in a jar file?
 

Griffin

Bekanntes Mitglied
If your program is finished, then you can put it in a jar file. So it gets started just by clicking on it. And the .class file can be used for other programs if there are any usefull methods or variables :p
 

cd-rw

Mitglied
Griffin hat gesagt.:
If your program is finished, then you can put it in a jar file. So it gets started just by clicking on it. And the .class file can be used for other programs if there are any usefull methods or variables :p

Could you (or someone else) do me a big favor? Run this code and see what output you get.

This is the class file I get when I compile the code (I had to zip it so the upload site would accept it):
http://upl.silentwhisper.net/uplfolders/upload0/HeaderTest.zip

Code:
import java.net.URL;
import java.net.URLConnection;

public class HeaderTest {
    public static void main(String[] args) {

        try {
    // Create a URLConnection object for a URL
    URL url = new URL("http://www.mod-x.co.uk: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) {
}

    }
}
 

Griffin

Bekanntes Mitglied
I copied the code into a file and I also tried to start your file. In both cases the are no errors but also nothing visual happens. Of course not, there is nothing in your code that would do an output.
But you have a for-lopp like this
Code:
for (int i=0; ; i++)
don't you need something like that
Code:
for (int i=0; i<somethingbigger ; i++)

Othwerwise the loop won't do anything!?!?
Ok, it works! Forget it.
And the loop is done 11 times.
 
G

Guest

Gast
Griffin hat gesagt.:
I copied the code into a file and I also tried to start your file. In both cases the are no errors but also nothing visual happens. Of course not, there is nothing in your code that would do an output.
But you have a for-lopp like this
Code:
for (int i=0; ; i++)
don't you need something like that
Code:
for (int i=0; i<somethingbigger ; i++)

Othwerwise the loop won't do anything!?!?
Ok, it works! Forget it.
And the loop is done 11 times.

Did you get it to run? In that case what was the output?
 

cd-rw

Mitglied
Anonymous hat gesagt.:
Griffin hat gesagt.:
I copied the code into a file and I also tried to start your file. In both cases the are no errors but also nothing visual happens. Of course not, there is nothing in your code that would do an output.
But you have a for-lopp like this
Code:
for (int i=0; ; i++)
don't you need something like that
Code:
for (int i=0; i<somethingbigger ; i++)

Othwerwise the loop won't do anything!?!?
Ok, it works! Forget it.
And the loop is done 11 times.

Did you get it to run? In that case what was the output?

That was my posting.
 

Griffin

Bekanntes Mitglied
Maybe i haven't wrote it precisely enough.
Both class files worked !!! But there was no output no errors or anything else. So i added a line like this:
Code:
System.out.println("Programm Output");
I added the code into the loop. And it appeared 11 times.
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
J Open reading Frame Java Basics - Anfänger-Themen 4
M slide effekt über page legen per klick(href) Java Basics - Anfänger-Themen 5
G mit "page" in JSP importieren Java Basics - Anfänger-Themen 13
J Portlet: JSP page redirection Java Basics - Anfänger-Themen 2
B Java Server Page Problem Java Basics - Anfänger-Themen 3
D page forward (?) suche syntax Java Basics - Anfänger-Themen 25
richis-fragen JTable Header ausgeblendete (width = 0) nicht per mouseDragged aufziehen. Java Basics - Anfänger-Themen 9
P Http Header, Http Proxy Java Basics - Anfänger-Themen 19
C Klassen JTable wird ohne Header aufgebaut Java Basics - Anfänger-Themen 6
S Header- Datei erzeugen mit javah Java Basics - Anfänger-Themen 1
F JButton wie selektierter JTabbebPane-Header Java Basics - Anfänger-Themen 7
K JTable AbstractTableModel Header setzen Java Basics - Anfänger-Themen 6
T iText Header Java Basics - Anfänger-Themen 3
H Eclipse Sync ohne File Header Java Basics - Anfänger-Themen 2
S Header von JTable lesen Java Basics - Anfänger-Themen 6
J JTable : Ausrichtung der Header Java Basics - Anfänger-Themen 4
C Antwort header anzeigen Java Basics - Anfänger-Themen 5
H Header von Textfile bearbeiten Java Basics - Anfänger-Themen 14
D JTable Header überschreiben. Java Basics - Anfänger-Themen 5
A table. kein header. kein scrollpane. Java Basics - Anfänger-Themen 5
G ersatz für header.h in java ? swict-case kanns nicht richtig Java Basics - Anfänger-Themen 6
S jTable - Header entfernen/ausblenden Java Basics - Anfänger-Themen 2
S Auslesen von Komponente aus Header Java Basics - Anfänger-Themen 2
G Header auslesen. Problem. Java Basics - Anfänger-Themen 40
N Header für Mail Java Basics - Anfänger-Themen 5
F Eigener IP-Paket-Header Java Basics - Anfänger-Themen 15
B File Header mit Java einlesen. Java Basics - Anfänger-Themen 9

Ähnliche Java Themen

Neue Themen


Oben