google currency converter code

Some times for our site we do neet to convert currency , here is the updated version i took from one blog and modified it in C#.net. 
You just have to pass Currency from (etc PKR) , currency to (etc $)
Then it will give you the exact amount
So no need to worried about update your currency every day. 
The code call the google api to convert currency

public static string ConvertCurrency (string To , string Amount , HttpRequest Request)
        {


        string Expression = Amount + “USD” + “=?” + To;
        string url = “http://www.google.com/ig/calculator?hl=en&q=” + Expression;


        string response = “”;
        string responseMsg = Request.Params.ToString();
        string post = responseMsg;
       
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = “POST”;
        req.ContentType = “application/x-www-form-urlencoded”;
        req.ContentLength = post.Length;


        StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
        writer.Write(post);
        writer.Close();


        StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream());
        response = reader.ReadToEnd();
        reader.Close();


        char[] cChar = new  char[3];
        string[] _params = new string[100];


        cChar[0] = ‘,’;
        _params = response.Split(cChar[0]);


        string ConvertedAmount = “”;


        ConvertedAmount = _params[1];
        ConvertedAmount = ConvertedAmount.Replace(“\””, “”);
        ConvertedAmount = ConvertedAmount.Replace(“rhs”, “”);
        ConvertedAmount = ConvertedAmount.Replace(“:”, “”);
        ConvertedAmount = ConvertedAmount.Trim();
        ConvertedAmount = ConvertedAmount.Remove(ConvertedAmount.IndexOf(‘ ‘), ConvertedAmount.Length – ConvertedAmount.IndexOf(‘ ‘));


        return ConvertedAmount;


        }

1 thought on “google currency converter code”

Leave a Reply

Your email address will not be published. Required fields are marked *