Писал одно время, посмотри мой код:
Код | public struct vkApiRequest { public string api_id; public string method; public string sig; public string version; public string format; public string sid; public string mid; public string secret; }
class vkApiManager { private vkApiRequest CurrentApiRequest; public vkApiManager(string mid,string sid, string secret) { CurrentApiRequest.api_id = "api_id=194932"; // <---------------Укажи свой ApplicationID CurrentApiRequest.version = "v=3.0"; CurrentApiRequest.format = "format=xml"; CurrentApiRequest.mid = mid; CurrentApiRequest.sid = "sid="+sid; CurrentApiRequest.secret = secret; }
private string MD5Hash(string instr) { string strHash = string.Empty;
foreach (byte b in new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(instr))) { strHash += b.ToString("X2"); } return strHash.ToLower(); } private string CreateSignature(string param) { string ForSign = CurrentApiRequest.mid; ForSign += CurrentApiRequest.api_id; ForSign += param; ForSign += CurrentApiRequest.format; ForSign += CurrentApiRequest.method; ForSign += CurrentApiRequest.version; ForSign += CurrentApiRequest.secret;
return MD5Hash(ForSign); } public string GetFriendsWithInfo() {
StringBuilder request_content = new StringBuilder();
CurrentApiRequest.method = "method=friends.get"; const string param = "fields= nickname, sex, bdate, city, photo"; request_content.Append("http://api.vkontakte.ru/api.php?"); request_content.Append(CurrentApiRequest.api_id); request_content.Append("&"+param+"&"); request_content.Append(CurrentApiRequest.format); request_content.Append("&"); request_content.Append(CurrentApiRequest.method); request_content.Append("&"); request_content.Append(CurrentApiRequest.sid); request_content.Append("&"); request_content.Append("sig=" + CreateSignature(param)); request_content.Append("&"); request_content.Append(CurrentApiRequest.version);
HttpWebRequest request = HttpWebRequest.Create(request_content.ToString()) as HttpWebRequest; request.Method = "POST";
request.AllowAutoRedirect = false;
HttpWebResponse response = request.GetResponse() as HttpWebResponse; StreamReader myStreamReader = new StreamReader(response.GetResponseStream()); string ret = myStreamReader.ReadToEnd(); response.GetResponseStream().Close();
return ret;
} }
|
|