/// <summary> /// 获取get请求中的字节流数据 /// </summary> /// <param name="smartQQAPI"></param> /// <returns></returns> public Stream GetStreamAsync(SmartQQAPI smartQQAPI, params object[] ags) { Uri refuri = addRef(smartQQAPI); Stream result = httpClient.GetStreamAsync(smartQQAPI.BuildUrl(ags)).Result; reciveRef(refuri); return(result); }
/// <summary> /// 获取get请求中的字节流数据 /// </summary> /// <param name="smartQQAPI"></param> /// <returns></returns> public byte [] GetBytesAsync(SmartQQAPI smartQQAPI, params object[] ags) { Uri refuri = addRef(smartQQAPI); byte[] result = httpClient.GetByteArrayAsync(smartQQAPI.BuildUrl(ags)).Result; reciveRef(refuri); return(result); }
/// <summary> /// 获取get请求的json /// </summary> /// <param name="smartQQAPI"></param> /// <returns></returns> public JObject GetJsonAsync(SmartQQAPI smartQQAPI, params object[] ags) { Uri refuri = addRef(smartQQAPI); string result = httpClient.GetStringAsync(smartQQAPI.BuildUrl(ags)).Result; reciveRef(refuri); return((JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result)); }
/// <summary> /// 获取get请求 /// </summary> /// <param name="smartQQAPI"></param> /// <returns></returns> public HttpResponseMessage GetAsync(SmartQQAPI smartQQAPI, params object[] ags) { Uri refuri = addRef(smartQQAPI); HttpResponseMessage result = httpClient.GetAsync(smartQQAPI.BuildUrl(ags)).Result; reciveRef(refuri); return(result); }
/// <summary> /// 添加Referrer /// </summary> /// <param name="smartQQAPI"></param> private Uri addRef(SmartQQAPI smartQQAPI) { var httpRef = httpClient.DefaultRequestHeaders.Referrer; if (smartQQAPI.Referer != null && !smartQQAPI.Referer.Equals("")) { httpClient.DefaultRequestHeaders.Referrer = new Uri(smartQQAPI.Referer); } return(httpRef); }
/// <summary> /// Post请求获取返回json /// </summary> /// <param name="smartQQAPI"></param> /// <param name="content"></param> /// <param name="retryTimes">重新尝试次数</param> /// <returns></returns> public JObject PostJsonAsync(SmartQQAPI smartQQAPI, JObject content, int retryTimes) { HttpResponseMessage httpResponseMessage = null; httpResponseMessage = PostResponseAsync(smartQQAPI, content).Result; while (httpResponseMessage.StatusCode != HttpStatusCode.OK && retryTimes-- >= 0) { httpResponseMessage = PostResponseAsync(smartQQAPI, content).Result; } return(Newtonsoft.Json.JsonConvert.DeserializeObject(httpResponseMessage.Content.ReadAsStringAsync().Result) as JObject); }
/// <summary> /// 获取对应url中的对应name的cookie /// </summary> /// <param name="url"></param> /// <param name="name"></param> /// <returns></returns> public string GetCookieByUrl(SmartQQAPI smartQQAPI, string name, params object [] ags) { CookieCollection cookies = null; if (ags.Length != 0) { cookies = CookieContainer.GetCookies(new Uri(smartQQAPI.BuildUrl(ags))); } else { cookies = CookieContainer.GetCookies(new Uri(smartQQAPI.Url)); } foreach (Cookie item in cookies) { if (item.Name.Equals(name)) { return(item.Value); } } return(""); }
/// <summary> /// Post请求 /// </summary> /// <param name="smartQQAPI"></param> /// <param name="content"></param> /// <returns></returns> public async Task <HttpResponseMessage> PostResponseAsync(SmartQQAPI smartQQAPI, JObject content) { var hasOrigin = httpClient.DefaultRequestHeaders.TryGetValues("Origin", out IEnumerable <string> origin); Uri refrui = addRef(smartQQAPI); if (httpClient.DefaultRequestHeaders.Contains("Origin")) { httpClient.DefaultRequestHeaders.Remove("Origin"); httpClient.DefaultRequestHeaders.Add("Origin", smartQQAPI.Origin); } else { httpClient.DefaultRequestHeaders.Add("Origin", smartQQAPI.Origin); } string contentstring = $"r={WebUtility.UrlEncode(content.ToString(Formatting.None))}"; HttpContent hc = new StringContent(contentstring, Encoding.UTF8); hc.Headers.ContentType = MediaTypeHeaderValue.Parse("application/ x-www-form-urlencoded; charset=UTF-8"); var response = httpClient.PostAsync(smartQQAPI.Url, hc); response.Wait(); // 复原httpClient if (hasOrigin) { httpClient.DefaultRequestHeaders.Remove("Origin"); httpClient.DefaultRequestHeaders.Add("Origin", origin); } else { httpClient.DefaultRequestHeaders.Remove("Origin"); } reciveRef(refrui); return(await response); }
/// <summary> /// 发送消息 /// </summary> /// <param name="messageType">消息类型</param> /// <param name="id">消息发送方向的id</param> /// <param name="msg">消息内容</param> /// <returns></returns> internal bool SendMessage(Message.MessageType messageType, long id, string msg) { try { SmartQQAPI url = null; switch (messageType) { case Message.MessageType.PrivateMessage: url = SmartQQAPI.SendMessageToFriend; break; case Message.MessageType.GroupMessage: url = SmartQQAPI.SendMessageToGroup; break; case Message.MessageType.UnKnowMessage: Log.Write("未知的消息类型!"); return(false); default: Log.Write("未知的类型!"); return(false); } var response = client.PostJsonAsync(url, new JObject { { SmartQQStaticString.GetParamNameByMessageType(messageType), id }, { "content", new JArray { SmartQQStaticString.TranslateEmoticons(msg), new JArray { "font", JObject.FromObject(Font.DefaultFont) } } .ToString(Formatting.None) }, { "face", 573 }, { "clientid", clientid }, { "msg_id", messageId++ }, { "psessionid", psessionid } }, RetryTimes); int?code = response["retcode"].ToObject <int?>(); if (code == 0) { Log.Write("消息发送成功!"); return(true); } else { return(false); } } catch (Exception) { Log.Write("发送信息失败!"); return(false); } return(true); }
/// <summary> /// Post请求获取返回json /// </summary> /// <param name="smartQQAPI"></param> /// <param name="content"></param> /// <returns></returns> public JObject PostJsonAsync(SmartQQAPI smartQQAPI, JObject content) { HttpResponseMessage httpResponseMessage = PostResponseAsync(smartQQAPI, content).Result; return(Newtonsoft.Json.JsonConvert.DeserializeObject(httpResponseMessage.Content.ReadAsStringAsync().Result) as JObject); }
/// <summary> /// Post请求获取返回字符串 /// </summary> /// <param name="smartQQAPI"></param> /// <param name="content"></param> /// <returns></returns> public string PostStringAsync(SmartQQAPI smartQQAPI, JObject content) { HttpResponseMessage httpResponseMessage = PostResponseAsync(smartQQAPI, content).Result; return(httpResponseMessage.Content.ReadAsStringAsync().Result); }