Buy Me A Beer Coffee For Blogger

I have a buy me a beer / coffee plugin for wordpress but my blogger blogs do not have one so I thought about scouring the net to look for one. I came across some that required you to place a code and changing some values. I did not like it though because it would not let me save the blog’s template because of some format problem. I could not understand what the problem was so I tried to look for other alternatives. Luckily, I came across Buckdrop. There is no registration necessary. Just fill out a form like your PayPal email address and a few clicks here and there with the options and you will be given a generated URL. You can use that URL to place it within your blogger template.

Make sure enable widget is checked then look for the code

. Place your buy me link below that, then save. Now you have that buy me link after every post in your blog. Pretty simple and straightforward. The only downside to this though, is if Buckdrop’s site is down. But well, I bet that rarely happens.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(1 votes, average: 5.00 out of 5)
 Loading ...

Sort, Filter Height In MySQL

I came across a situation where I had to filter out a height column so that I will only get rows between, say 5′6 as the minimum. Now, if let us say you are required to filter out all rows with a height between 5′3 and 6′5, you cannot possibly enumerate each height in the IN keyword of SQL. Well, you can, but that is not maximizing the capabilities of programming then. What you can do is extract the values of the height column and get the feet and inches value respectively. With that, you can multiply the foot value by 12 since 1 foot is equal to 12 inches, then add the remaining inches to the resulting product.

Using the MySQL function substring_index(), you can retrieve a substring value at the specified index. So for example, if your height values are in this format 5′6, you can do this in MySQL …

where (substring_index(height, "'", 1) * 12 + substring_index(height, "'", -1)) >= 66

The MySQL query above filters out rows that have a minimum height of 5′6.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

A Useful Java Tool For Storing Cookies

I had this post on how to login to sites using Java’s URL and HttpURLConnection classes and how there is no need to manually store a cookie’s information and pass it to every other URL you intend to visit that requires a login if it cannot find the cookie information. This neat Cookie utility by Saar Machtinger handles all storing of cookie information after you log in to a site. It only needs to read the the contents of the HttpURLConnection variable after it has successfuly posted a request to login.

Then, once you have the CookieTool variable in hand with the cookie information like session id among others, you can access any url of that certain site which you logged in to. To do so, simply do this (ct is the CookieTool variable).

URL url = new URL("http://www.mydomain.com/some_page.jsp");
HttpURLConnection httpcon = (HttpURLConnection) ct.writeCookies(url.openConnection(), false);
String content = METHOD_THAT_CONVERT_INPUTSTREAM_TO_TEXT(httpcon.getInputStream());
httpcon.disconnect();

This is the CookieTool class by the way.

// Changes by: Saar Machtinger, me@cawa.com
import java.net.*;
import java.util.*;
 
public class CookieTool {
 
	static Hashtable theCookies = new Hashtable();
 
	/**
	* Send the Hashtable (theCookies) as cookies, and write them to
	*  the specified URLconnection
	*
	* @param   urlConn  The connection to write the cookies to.
	* @param   printCookies  Print or not the action taken.
	*
	* @return  The urlConn with the all the cookies in it.
	*/
	public URLConnection writeCookies(URLConnection urlConn, boolean printCookies){
		String cookieString = "";
		Enumeration keys = theCookies.keys();
 
		while (keys.hasMoreElements()) {
			String key = (String)keys.nextElement();
			cookieString += key + "=" + theCookies.get(key);
			if (keys.hasMoreElements()) cookieString += "; ";
		}
 
		urlConn.setRequestProperty("Cookie", cookieString);
 
		if (printCookies) System.out.println("Wrote cookies:\n   " + cookieString+"\n");
 
		return urlConn;
	}
 
	/**
	* Read cookies from a specified URLConnection, and insert them
	*   to the Hashtable
	*  The hashtable represents the Cookies.
	*
	* @param   urlConn  the connection to read from
	* @param   printCookies  Print the cookies or not, for debugging
	* @param   reset  Clean the Hashtable or not
	*/
 
	public void readCookies(URLConnection urlConn, boolean printCookies, boolean reset) {
		if (reset) theCookies.clear();
		int i=1;
		String hdrKey;
		String hdrString;
		String aCookie;
 
		while ((hdrKey = urlConn.getHeaderFieldKey(i)) != null) {
			if (hdrKey.equals("Set-Cookie")) {
				hdrString = urlConn.getHeaderField(i);
				StringTokenizer st = new StringTokenizer(hdrString,",");
 
				while (st.hasMoreTokens()) {
					String s = st.nextToken();
					aCookie = s.substring(0, s.indexOf(";"));
					// aCookie = hdrString.substring(0, s.indexOf(";"));
					int j = aCookie.indexOf("=");
 
					if (j != -1) {
						if (!theCookies.containsKey(aCookie.substring(0, j))) {
						   // if the Cookie do not already exist then when keep it,
						   // you may want to add some logic to update 
						   // the stored Cookie instead. thanks to rwhelan
						   theCookies.put(aCookie.substring(0, j),aCookie.substring(j + 1));
 
							if (printCookies) {
								System.out.println("Reading Key: "  + aCookie.substring(0, j));
								System.out.println("        Val: " + aCookie.substring(j + 1));
							}
						}
					}
				}
			}
			i++;
		}
	}
 
	/**
	* Display all the cookies currently in the HashTable
	*
	*/
	public void viewAllCookies() {
		System.out.println("All Cookies are:");
		Enumeration keys = theCookies.keys();
		String key;
		while (keys.hasMoreElements()) {
			key = (String)keys.nextElement();
			System.out.println("   " + key + "=" + theCookies.get(key));
		}
	}
 
	/**
	* Display the current cookies in the URLConnection,
	*    searching for the: "Cookie" header
	*
	* This is Valid only after a writeCookies operation.
	*
	* @param   urlConn  The URL to print the associates cookies in.
	*/
	public void viewURLCookies(URLConnection urlConn) {
		System.out.print("Cookies in this URLConnection are:\n");
		System.out.println(urlConn.getRequestProperty("Cookie"));
	}
 
	/**
	* Add a specific cookie, by hand, to the HastTable of the Cookies
	*
	* @param   _key  The Key/Name of the Cookie
	* @param   _val  The Calue of the Cookie
	* @param   printCookies  Print or not the result
	*/
	public void addCookie(String _key, String _val, boolean printCookies) {
		if (!theCookies.containsKey(_key)) {
			theCookies.put(_key, _val);
			if (printCookies){
				System.out.println("Adding Cookie: ");
				System.out.println("   " + _key + " = " + _val);
			}
		}
	}
 
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

Login To A Site Using Java

Logging in to a site using pure Java code is possible with just the URL and HttpURLConnection classes. First, you need to know the action URL of the <form> tag in a website that you wish to login to. Next, look for all input field tags within the <form> tag because they will be needed to be passed as parameters when you submit to the form URL, e.g. in this case, logging in to the site. Every input field with a name attribute should be passed just to be sure. Suppose we have a sample form that looks like this.

<form action="login.jsp">
	<input type="text" name="username" size="20"/><br/>
	<input type="text" name="password" size="20"/><br/>
	<input type="submit" value="login"/>
</form>

To log in using Java programmatically, use the following code.

URL url = new URL("http://www.mydomain.com/login.jsp");
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
String data = "username=MYUSERNAME&password=MYPASSWORD";
httpcon.setDoOutput(true);
 
OutputStreamWriter wr = new OutputStreamWriter(httpcon.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
httpcon.setInstanceFollowRedirects(false);
 
CookieTool ct = new CookieTool();
ct.readCookies(httpcon, false, false);
httpcon.disconnect();

That is it. Any other page you wish to go to, just use the CookieTool variable which contains everything that the generated cookie contains like session id. What is CookieTool exactly for? Go here to find more about how to use this neat Cookie utility to store your cookie sessions so that which page you would go to the site that requires a login, you do not need to log in again because the cookie information is stored in the CookieTool variable.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(1 votes, average: 5.00 out of 5)
 Loading ...