translate HTML tag to String

Discussion in 'Plugin Development' started by The_Spaceman, Mar 29, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    The_Spaceman

    hello,

    Code:
    private static String getWebTitle(String site) {
    InputStream response = null;String title = null; try {
    response = new URL(site).openStream();Scanner scanner = new Scanner(response);String responseBody = scanner.useDelimiter("\\A").next();title = (responseBody.substring(responseBody.indexOf("<title>") + 7, responseBody.indexOf("</title>")));} catch (Throwable ignore) {
    } finally {
    try {
    if (response != null) {
    response.close();}
    } catch (Throwable ignore) {
    }
    }
    
    return title;}
    
    the code above gets the title of a web page, but this doesn't always works...

    when the title contains special characters it wont get translated correctly

    for example:
    title is: 'ɹǝdsɐſ'
    translation: 'ɹ�ds�ſ'

    is there a way to correct this?

    thanks in advanced :D
     
  2. Offline

    timtower Administrator Administrator Moderator

    @The_Spaceman Might be that you need to run an url decoder on that string.
     
  3. Offline

    The_Spaceman

    I have this:

    Code:
    ...
    response = new URL(site).openStream();
    Scanner scanner = new Scanner(response);
    String responseBody = scanner.useDelimiter("\\A").next();
    title = (responseBody.substring(responseBody.indexOf("<title>") + 7, responseBody.indexOf("</title>")));
    title = URLDecoder.decode(title, "UTF-8");
    ...
    
    
    but it still doesn't work, got any more ideas?
     
Thread Status:
Not open for further replies.

Share This Page