Convert 2 lines of Php to Zenno C#

Stroks

Client
Регистрация
09.02.2012
Сообщения
219
Благодарностей
14
Баллы
18
How do I convert these 2 lines of code to Zenno C#.
Код:
$binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true)

$urlSafeSignature = urlencode(base64_encode($binarySignature))
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
Stroks, what API is this for because I might have it encoded in C# already. Is it for Moz? Twitter?
 
  • Спасибо
Реакции: Stroks

Stroks

Client
Регистрация
09.02.2012
Сообщения
219
Благодарностей
14
Баллы
18
It is Moz php from this url - http://apiwiki.moz.com/php.

There are 2 version - Signed Authentication and Batching URLs. I would probably prefer Batching URLs if this can be done in C# and if you have it as because it returns data for 10 urls at once so it should be much faster. However if the Batching URLs method cannot be done with C# even Signed Authentication method is ok.
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
Here is the Signed for everyone that needs it. You will have to regex out of the result what you need like PA and DA.

You will also need to add references to System.Web and System.Security to your GAC.

Код:
string proxy = project.Variables["Proxy"].Value;
string memberID = project.Variables["mozMemberID"].Value;
string keyString = project.Variables["mozSecret"].Value;
string requestUri = "http://lsapi.seomoz.com/linkscape/url-metrics/";
string domain = System.Web.HttpUtility.UrlEncode(project.Variables["url"].Value);
double add = 15;
TimeSpan tsDuration = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));

            // Add intExpiryHours to the duration
            TimeSpan tsEnd = TimeSpan.FromMinutes(add);
            tsDuration = tsDuration.Add(tsEnd);

            // Generate our unix timestamp
            double dblUnixTime = tsDuration.TotalSeconds;

            // Remove the decimal parts, we don't need them
            int intUnixTime = (int)dblUnixTime;

            // Convert to string for the api request
            string strExpiry = intUnixTime.ToString();
		string message = string.Concat(memberID + "\n" + strExpiry); 
ASCIIEncoding encoding = new ASCIIEncoding();
            // Convert our message and keystring to bytes
            byte[] keyByte = encoding.GetBytes(keyString);
            byte[] messageBytes = encoding.GetBytes(message);

            // Compute the hash
            System.Security.Cryptography.HMACSHA1 algorithm = new System.Security.Cryptography.HMACSHA1(keyByte);
            byte[] hash = algorithm.ComputeHash(messageBytes);

            // Base64 encode the hash
            string strSignature = Convert.ToBase64String(hash);

            // Return a url encoded version of the encoded hash string
            string signature =  System.Web.HttpUtility.UrlEncode(strSignature);
			
			string strAPIUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/" + domain + "?AccessID=" + memberID + "&Expires=" + strExpiry + "&Signature=" + signature;
            
		
			string resultHttpGet = ZennoPoster.HttpGet(strAPIUrl, proxy, "UTF-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.HeaderAndBody);

			return resultHttpGet;
 
  • Спасибо
Реакции: myndeswx и Stroks

Stroks

Client
Регистрация
09.02.2012
Сообщения
219
Благодарностей
14
Баллы
18
Batching url code addition:

Код:
//request url
string strAPIUrl1 = "http://lsapi.seomoz.com/linkscape/url-metrics/?AccessID=" + memberID + "&Expires=" + strExpiry + "&Signature=" + signature;
           
			//create json array manually out of a list.
			var myjson = "[";
			for (int i=0;i<sourceList.Count;i++) {
			myjson = myjson + "\""+ sourceList[i]+ "\",";
			//System.Web.HttpUtility.UrlEncode(	
			}
			myjson = myjson + "]";
			myjson = myjson.Replace(",]","]"); 
		
			
var resultHttpPost = ZennoPoster.HttpPost(strAPIUrl1,myjson,"application/json; charset=utf-8", proxy, "iso-8859-1", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly);
 
  • Спасибо
Реакции: bigcajones

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)