/// <summary> /// 获取授权Token /// </summary> /// <param name="requestUrl">API请求地址</param> /// <param name="parameters"></param> /// <param name="responseData"></param> /// <returns></returns> private bool GetOAuthToken(string requestUrl, Parameters parameters, out NameValueCollection responseData) { this.LastError = null; responseData = null; parameters.Add("oauth_consumer_key", this.AppKey); parameters.Add("oauth_signature_method", "HMAC-SHA1"); parameters.Add("oauth_timestamp", Util.GenerateTimestamp()); parameters.Add("oauth_nonce", Util.GenerateRndNonce()); parameters.Add("oauth_version", "1.0"); parameters.Add("oauth_signature", this.GenerateSignature("GET", requestUrl, parameters)); string url = string.Concat(requestUrl, "?", parameters.BuildQueryString(true)); SyncHttpRequest request = new SyncHttpRequest(url, this.Charset); try { string response = request.Get(); if (!string.IsNullOrEmpty(response)) { responseData = HttpUtility.ParseQueryString(response, this.Charset); this.Token = responseData["oauth_token"]; this.TokenSecret = responseData["oauth_token_secret"]; return(!string.IsNullOrEmpty(this.Token) && !string.IsNullOrEmpty(this.TokenSecret)); } } catch (Exception ex) { this.LastError = ex; } return(false); }
/// <summary> /// 请求并获取输出的数据 /// </summary> /// <param name="requestMethod">请求方法.如GET或POST</param> /// <param name="requestUrl">API请求地址</param> /// <param name="parameters">参数</param> /// <param name="files">上传的文件</param> /// <returns></returns> protected virtual T GetResponseData <T>(string requestMethod, string requestUrl, Parameters parameters, Files files) where T : ResponseObject { this.LastError = null; this.AddOAuthParameter(requestMethod, requestUrl, parameters); bool isPost = "POST".Equals(requestMethod, StringComparison.OrdinalIgnoreCase); SyncHttpRequest request = new SyncHttpRequest(requestUrl, this.OAuth.Charset); request.Parameters = parameters; string data = string.Empty; try { if (isPost) { if (files != null) { data = request.PostFile(files); } else { data = request.Post(); } } else { data = request.Get(); } } catch (Exception ex) { this.LastError = ex; data = string.Empty; } if (string.IsNullOrEmpty(data)) { return(default(T)); } XmlDocument xml = new XmlDocument(); xml.LoadXml(data); return(ResponseObject.CreateInstance <T>(xml.DocumentElement)); }