-1

很长一段时间以来,我一直在解析 HTTP 中的一些信息。

我不明白的事情:有些行可以解析,其他行不能!

public String parse(String arg) throws IOException {
    scanner = new Scanner(req);
    StringBuilder buildValue = new StringBuilder();
    boolean lineFound = false;
    int sf;
    while (scanner.hasNextLine()) {
        thisLine = new StringReader(scanner.nextLine());
        searchFor = new StringReader(arg);
        lineFound = false;
        for (int tl; (tl = thisLine.read()) != -1;) {
            sf = searchFor.read();
            if (sf != -1) {
                if (sf == tl && !lineFound) {
                    builder.append((char)tl);
                }
            } else {
                if (lineFound) {
                    buildValue.append((char)tl);
                }
                if (!lineFound && builder.toString().equals(arg)) {
                    builder.delete(0, builder.capacity());
                    System.out.println("GEFUNDEN");
                    lineFound = true;
                }
            }
        }
    }
    thisLine.close();
    searchFor.close();
    builder.delete(0, builder.capacity());
    String parsed = buildValue.toString();
    buildValue.delete(0, buildValue.capacity());
    return parsed;
}

GET / HTTP/1.1

Host: xxx.xxx.xxx.xxx:xxxx

Connection: keep-alive

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/30.0.1599.101 Safari/537.36

Accept-Encoding: gzip,deflate,sdch

Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4*

唯一可以解析的行: - 主机: - 接受:

看不懂了,所以想在这里问一下。

最好的问候, 埃尔克森

编辑:

我忘记了字段:

private String req;
private Scanner scanner;
private StringReader reader;
private StringReader searchFor;
private StringReader thisLine;
private StringBuilder builder = new StringBuilder();
4

1 回答 1

0

您应该使用Apache 的 HttpComponents 库。

于 2013-11-02T18:02:01.030 回答