/// <summary> /// 从 AliMQ 拉取消息 /// </summary> /// <returns>消息列表</returns> private List <OnsConsumerResult> PullMessageFromAli() { List <OnsConsumerResult> result = null; long time = GetCurrentTimeMillis(); //数据签名 String signString = TopicID + NEWLINE + ConsumerID + NEWLINE + time; string sign = SignString(signString, Ons_SecretKey); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("Signature", sign); headers.Add("AccessKey", Ons_AccessKey); headers.Add("ConsumerID", ConsumerID); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("topic", TopicID); parameters.Add("time", time.ToString()); parameters.Add("num", 32.ToString()); parameters.Add("Signature", sign); HttpWebResponseEx res = WebHttpHepper.SendRequest(Url, "GET", headers, parameters); result = JSON.Deserialize <List <OnsConsumerResult> >(res.RetureValue); return(result); }
/// <summary> /// 删除 aliMQ 上的消息 /// </summary> /// <param name="msgHandle">msgHandle 查询时返回对象的属性</param> /// <returns>是否成功</returns> private bool DeleteMessage(String msgHandle) { long time = GetCurrentTimeMillis(); String signString = TopicID + NEWLINE + ConsumerID + NEWLINE + msgHandle + NEWLINE + time; string sign = SignString(signString, Ons_SecretKey); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("Signature", sign); headers.Add("AccessKey", Ons_AccessKey); headers.Add("ConsumerID", ConsumerID); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("topic", TopicID); parameters.Add("time", time.ToString()); parameters.Add("timeout", "300000"); parameters.Add("msgHandle", msgHandle); HttpWebResponseEx res = WebHttpHepper.SendRequest(Url, "DELETE", headers, parameters); if (res.StatusCode == HttpStatusCode.NoContent) { Console.WriteLine("delete message success: {0}{1}", msgHandle, res.RetureValue); return(true); } return(false); }
/// <summary> /// 传入URL返回网页的html代码带有证书的方法 /// </summary> /// <param name="url">URL</param> /// <returns></returns> public static HttpWebResponseEx SendRequest(string url, string method, Dictionary <string, string> headers = null, Dictionary <string, string> parameters = null, string postData = null) { StringBuilder urlBuilder = new StringBuilder(url); urlBuilder.Append(ConcatQueryString(parameters)); url = urlBuilder.ToString(); HttpWebRequest request = null; if (url.Contains("https")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { request = (HttpWebRequest)WebRequest.Create(url); } // 与指定URL创建HTTP请求 request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)"; request.Method = method; request.Accept = "*/*"; //headers foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } if (method.ToUpper() == "POST") { byte[] data = Encoding.UTF8.GetBytes(postData); request.ContentType = "text/plain;charset=UTF-8"; request.ContentLength = data.Length; using (Stream steam = request.GetRequestStream()) { steam.Write(data, 0, data.Length); } } // 获取对应HTTP请求的响应 HttpWebResponseEx res = new HttpWebResponseEx(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); res.StatusCode = response.StatusCode; // 对接响应流(以"UTF8"字符集) using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { res.RetureValue = reader.ReadToEnd(); } return(res); }