Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to decode URL data?

807603Oct 7 2007 — edited Oct 23 2007
How do I decoded encoded url data without remaking all the functions I already remade em actually but they mess up the decryption even more.

Lets say encoded it looks like this (I added spaces because it auto decodes in this topic too)

7 &# 56 ; &# 46 ;6 &# 49 ;.99.22 &# 49 ;

decoded it looks like this.

78.61.99.221

bascially &# decimal ; ender

and html converts decimal back to ascii.


At first I used

name = new URLDecoder().decode((String)name.trim(),"utf-8");

It didn't work because it doesn't support Hexadecimal yes page contains some hexadecimal color definations.

I started doing this the hard way and decoding is a bit off..

	String decodeTheDecimals(String original) {
    		StringBuilder result = new StringBuilder(original.length());
    		String delimiters = "&#";
    		StringTokenizer st = new StringTokenizer(original, delimiters, false);
    		while (st.hasMoreTokens()) {
        		String w = st.nextToken();
			if(w.indexOf(';') > -1) {
				w = w.substring(0, 3);
				int decoded = extract(w);
				if(decoded != -1)
					result.append(String.valueOf((char)decoded));
			} else {
				result.append(w);
			}
    		}
    		return result.toString();
	}

	public int extract(String s) {
		int j = s.length()-1;
		while (j >= 0 && Character.isDigit(s.charAt(j))) {
			j--;
		}
		try {
			int number = Integer.parseInt(s.substring(j+1,s.length()));
			return number;
		} catch(java.lang.NumberFormatException e) {
			return -1;
		}
	}
what would be the best way to decode it? the website is
http://moparscape.org/serverstatus.php

All I want to do is make a program that dumps server ip : port's to txt file.
	private void loadServerList() throws Exception {
		URL url = new URL("http://moparscape.org/serverstatus.php");
		URLConnection urlopen = url.openConnection();
		urlopen.setRequestProperty("User-agent","Mozilla/4.0");

		BufferedReader in = new BufferedReader(new InputStreamReader(urlopen.getInputStream()));
		String name;
		int currentSpot = 0;
		while ((name = in.readLine()) != null) {
			try {
				name = decodeTheDecimals(name);
				//name = new URLDecoder().decode((String)name.trim(),"utf-8");
				System.out.println("line #" + currentSpot + " " + name);
currentSpot++;
			} catch(ArrayIndexOutOfBoundsException e) {
				System.out.println("Servers loaded");
				break;
			}
		}
		System.out.println("servers loaded");
		in.close();
	}
Any help please man this is really hard on me.

Edited by: protosstribe on Oct 7, 2007 2:26 AM

Edited by: protosstribe on Oct 7, 2007 2:27 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 20 2007
Added on Oct 7 2007
10 comments
673 views