Jun 20, 2013

Java: How to get cookie from HttpURLConnection

Here the simple code to get the site response cookie.

URL urlReq = new URL("http://www.google.com");

conn = (HttpURLConnection) urlReq.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(8000);
conn.setConnectTimeout(8000);

// Start building other http request parameter
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Firefox/21.0");
conn.setRequestProperty("Accept-Language","en-us,en;q=0.5");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Connection", "close");

conn.setDoOutput(true);
conn.setDoInput(true);

conn.connect();
resCode = conn.getResponseCode(); 
if (resCode==HttpURLConnection.HTTP_OK){
 for(Map.Entry<String, List<String>> headers : conn.getHeaderFields().entrySet()){
 if (headers.getKey().equals("set-cookie")){
      logger.info("\t==>"+headers.getKey());
      for(String hval : headers.getValue()){
       logger.info("\t\t==>"+hval);
      }
 }else{
  logger.info("\t==>"+headers.getKey() +" = "+conn.getHeaderField(headers.getKey()));
 }
}





You can also use
conn.getHeaderField("Set-Cookie")
However, the bad thing is you can one get one cookie, I suggest to use
conn.getHeaderFields()

Happy Reading

Jun 5, 2013

How to determine the user browser version in html?

Since this code, is only application in IE(Internet Explorer). Maybe the question is, How to determine the browser version in html.

This this check is the user user IE Browser, whatever the version.
<!--[if IE]>
Place content here to target all Internet Explorer users.
<![endif]-->

This was opposite behavior on the first item. "This is only run when the browser is NOT Internet Explorer".
<![if !IE]>
Place content here to target all users not using Internet Explorer.
<![endif]>

This will run if the browser version is greater than or equal to 8.
<!--[if gte IE 8]>
Place content here to target users of Internet Explorer 8 or higher.
<![endif]-->

A similar example above that only runs when the browser version is less than 7 (i.e. 6 or lower).
<!--[if lt IE 7]>
Place content here to target users of Internet Explorer 6 or lower (less than 7).
<![endif]-->

Conclusion:
The code/conditions are only run when running on IE browser, otherwise the browser will detect/treat as normal comment.

May 15, 2013

How to charge iPad/iPhone on linux(OpenSuse)

Im not sure if this already in the package repository OpenSuse. Since Spec file already already found opensuse. As I say Im NOT SURE, But I want you to try if you are use the latest/later version of OpenSuse. Currently Im using OpenSuse 11.2.

Try to go in your YAST package and search "ipad_charge". If exists, your lucky. :) But If not. Try to compile it. Please follow instruction below.

Initial requirement:
1 libusb-1.x.x and libusb-1.x.x-dev
2. gcc


1. Download the ipad_charge source to Rainbow Software or ipad_charge.

2. Remember to Install libusb-1.x.x and libusb-1.x.x-dev.
3. Extract the downloaded source code of ipad_charge.
syntax3rror@syntax3rror:~/Utils> tar -xzf ~/Installer/ipad_charge_1.1.tar.gz
4. Change the directory where you extractthe source. For me I extract Utils.
syntax3rror@syntax3rror:~/Utils> cd ipad_charge-1.1/
5 Compile and install the source code.
make
sudo make install
That's it, your DONE.


Possible error encounter.
syntax3rror@syntax3rror:~/Utils/ipad_charge-1.1> make
gcc -Wall -Wextra ipad_charge.c -lusb-1.0 -o ipad_charge
ipad_charge.c:7:31: fatal error: libusb-1.0/libusb.h: No such file or directory
compilation terminated.
make: *** [ipad_charge] Error 1
This error encounter when you forgot to install libusb-1.x.x and libusb-1.x.x-dev.

May 2, 2013

magento: How to get the current login customer?

Get the customer session
$customer = Mage::getSingleton('customer/session')->getCustomer();

Get customer properties.
$customer->getId();
$customer->getEmail();
$customer->getName(); // Full name
$customer->getFirstName(); // First name
$customer->getLastName(); // Last name

You can also get other customer custom attribute, like nick_name
$customer->getNickName();


If you think you need to know the current admin user try this one.
Mage::getSingleton('admin/session')->getUser();