|
|
Home » Community » Coffee corner » C# -> U++ conversion...
C# -> U++ conversion... [message #34100] |
Tue, 18 October 2011 18:31  |
 |
mirek
Messages: 14257 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 
In this case, it is also a nice demonstration of difference between "properties" and U++...
Mirek
|
|
|
|
|
|
|
Goto Forum:
Current Time: Tue May 13 08:01:55 CEST 2025
Total time taken to generate the page: 0.00434 seconds
|
|
|