Search in sources :

Example 1 with HTTPRequest

use of org.cybergarage.http.HTTPRequest in project i2p.i2p by i2p.

the class Parser method parse.

// //////////////////////////////////////////////
// parse (URL)
// //////////////////////////////////////////////
public Node parse(URL locationURL) throws ParserException {
    String host = locationURL.getHost();
    int port = locationURL.getPort();
    // Thanks for Hao Hu
    if (port == -1)
        port = 80;
    String uri = locationURL.getPath();
    try {
        HttpURLConnection urlCon = (HttpURLConnection) locationURL.openConnection();
        // I2P mods to prevent hangs (see HTTPRequest for more info)
        // this seems to work, getInputStream actually does the connect(),
        // (as shown by a thread dump)
        // so we can set these after openConnection()
        // Alternative would be foo = new HttpURLConnection(locationURL); foo.set timeouts; foo.connect()
        urlCon.setConnectTimeout(2 * 1000);
        urlCon.setReadTimeout(1000);
        urlCon.setRequestMethod("GET");
        urlCon.setRequestProperty(HTTP.CONTENT_LENGTH, "0");
        if (host != null)
            urlCon.setRequestProperty(HTTP.HOST, host);
        InputStream urlIn = urlCon.getInputStream();
        Node rootElem = parse(urlIn);
        urlIn.close();
        urlCon.disconnect();
        return rootElem;
    } catch (Exception e) {
    // throw new ParserException(e);
    }
    HTTPRequest httpReq = new HTTPRequest();
    httpReq.setMethod(HTTP.GET);
    httpReq.setURI(uri);
    HTTPResponse httpRes = httpReq.post(host, port);
    if (httpRes.isSuccessful() == false)
        throw new ParserException("HTTP comunication failed: no answer from peer." + "Unable to retrive resoure -> " + locationURL.toString());
    String content = new String(httpRes.getContent());
    ByteArrayInputStream strBuf = new ByteArrayInputStream(content.getBytes());
    return parse(strBuf);
}
Also used : HTTPRequest(org.cybergarage.http.HTTPRequest) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HTTPResponse(org.cybergarage.http.HTTPResponse)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 HTTPRequest (org.cybergarage.http.HTTPRequest)1 HTTPResponse (org.cybergarage.http.HTTPResponse)1