public void TestBuilder() { var builder = new TsHelpers.UriBuilder(); builder.BaseUrl = "https://api.vkontakte.ru/method/notes.add"; Assert.IsNotNull(builder); Assert.IsNotNull(builder.BaseUrl); Assert.AreEqual("https://api.vkontakte.ru/method/notes.add", builder.BaseUrl); builder.Append("title", "Заголовок"); builder.Append("text", "Текст заметки"); Assert.AreEqual( @"https://api.vkontakte.ru/method/notes.add?title=%d0%97%d0%b0%d0%b3%d0%be%d0%bb%d0%be%d0%b2%d0%be%d0%ba&text=%d0%a2%d0%b5%d0%ba%d1%81%d1%82+%d0%b7%d0%b0%d0%bc%d0%b5%d1%82%d0%ba%d0%b8", builder.Uri); }
public virtual JObject Execute(string methodName) { requestBuilder = new Helpers.UriBuilder(); requestBuilder.BaseUrl = String.Format("{0}/{1}", apiUrl.Trim('/'), methodName.Trim('/')); requestBuilder.Append(Parameters); string result = string.Empty; try { var request = HttpWebRequest.Create(requestBuilder.Uri); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; var response = request.GetResponse(); using (var responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { result = responseReader.ReadToEnd(); } } catch (WebException wex) { using (var errorResponse = (HttpWebResponse)wex.Response) { using (var errReader = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8)) { result = errReader.ReadToEnd(); } } } JObject jsonResponse = JObject.Parse(result); var properties = jsonResponse.Children().Cast<JProperty>(); if (properties.Any(x => x.Name == VkConst.JsonError)) { var message = properties.Single(x => x.Name == VkConst.JsonErrorDescription).Value.ToString(); throw new Exception(message); } return jsonResponse; }