All pastes #627763 Raw Edit

Java.net

public java v1 · immutable
·published 2007-07-20 14:33 UTC
rendered paste body
// Address we will be usingprivate String _searchBase = "http://www.google.com/search?q=";public String singleSearch(String searchString){	try	{		searchString = URLEncoder.encode(searchString, "UTF-8");	}	catch (UnsupportedEncodingException e)	{		return e.toString();	}	String result = null;	HttpURLConnection huc = null;	try	{// create a URL from the base address plus what we want to search for.		URL url = new URL(_searchBase + searchString);//Create the connection		huc = (HttpURLConnection) url.openConnection();		huc.setRequestMethod("GET");		huc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; PircBot; JVM)");		huc.setRequestProperty("Pragma", "no-cache");		huc.connect();//Once connected we want to get whats on the screen.// huc.getInputSteam() will get the input stream so now we just need to read it.		BufferedReader reader = new BufferedReader(new InputStreamReader(huc.getInputStream()));		String line = null;		while ((line = reader.readLine()) != null)		{			Matcher matcher = google_pattern.matcher(line);			if (matcher.matches())			{				result = matcher.group(1) + " (" + matcher.group(2) + ")";				break;			}		}	}	catch (Exception e)	{		result = "Could not connect to google";	}	finally	{		if (huc != null)		{			huc.disconnect();		}	}	if (result != null)	{		result = result.replaceAll("<.*?>", "");		result = result.replaceAll("&.*?;", "");	}	else	{		result = "Google did not find anything";	}	return result;}