Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Coffee corner » C# -> U++ conversion...
C# -> U++ conversion... [message #34100] Tue, 18 October 2011 18:31 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Funny, today I had to rewrite some code (google authentization) from C# to U++:

C#:
private static string GetAuthToken() {
  WebRequest webRequest = HttpWebRequest.Create("https://www.google.com/accounts/ClientLogin");
  webRequest.Method = "POST";
  webRequest.ContentType = "application/x-www-form-urlencoded";
 
  string postParams =
      "accountType=" + HttpUtility.UrlEncode("GOOGLE") +
      "&Email=" + HttpUtility.UrlEncode("INSERT_LOGIN_EMAIL_HERE") +
      "&Passwd=" + HttpUtility.UrlEncode("INSERT_PASSWORD_HERE") +
      "&service=" + HttpUtility.UrlEncode("adwords") +
      "&source=" + HttpUtility.UrlEncode(string.Format("{0}-{1}-{2}", "YOUR_COMPANY_NAME",
          "YOUR_APP_NAME", "YOUR_VERSION_ID"));
 
  byte[] postBytes = Encoding.UTF8.GetBytes(postParams);
  webRequest.ContentLength = postBytes.Length;
 
  using (Stream strmReq = webRequest.GetRequestStream()) {
    strmReq.Write(postBytes, 0, postBytes.Length);
  }
 
  string retVal = "";
  try {
    WebResponse response = webRequest.GetResponse();
 
    using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
      string sResponse = reader.ReadToEnd();
      string[] splits = sResponse.Split('\n');
      foreach (string split in splits) {
        string[] subsplits = split.Split('=');
        if (subsplits.Length >= 2 && subsplits[0] == "Auth") {
          retVal = subsplits[1];
        }
      }
    }
  } catch (WebException ex) {
    throw new ApplicationException("Could not generate auth token.", ex);
  }
  return retVal;
}



U++:
String GetAuthToken() {
    String h =
        HttpsClient()
               .URL("https://www.google.com/accounts/ClientLogin")
               .Post("accountType=GOOGLE" 
                     "&Email=" + GetIniKey("google_email") +
                     "&Passwd=" + GetIniKey("google_passwd") +
                     "&service=adwords")
               .ExecuteRedirect();
        ;
    Vector<String> s = Split(h, '\n');
    for(int i = 0; i < s.GetCount(); i++)
        if(s[i].StartsWith("Auth="))
            return s[i].Mid(5);
    return Null;
}


It is childish, but this always makes me feel good Wink

In this case, it is also a nice demonstration of difference between "properties" and U++...

Mirek
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Web framework naming problem...
Next Topic: Intersrv/interlnk
Goto Forum:
  


Current Time: Tue Apr 23 18:12:11 CEST 2024

Total time taken to generate the page: 0.01887 seconds