推送的连接如:www.baidu.com
/// <summary> /// 推送XML /// </summary> /// <param name="msg">XML</param> /// <param name="httpUrl">推送的网址 链接</param> /// <returns></returns> public static string SendMsg(string msg, string httpUrl) { string result = ""; try { string url = msg; if (string.IsNullOrEmpty(httpUrl)) return "推送失败,Web.config配置为空"; string formData = url; //提交的参数 //注意提交的编码 这边是需要改变的 这边默认的是Default:系统当前编码 byte[] postData = Encoding.UTF8.GetBytes(formData); // 设置提交的相关参数 HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest; Encoding myEncoding = Encoding.UTF8; request.Method = "POST"; request.KeepAlive = false; request.Headers.Add("checksum", "61f6b6a01a6099d39352bdb4ffd77b97");//标头https-Header request.AllowAutoRedirect = true; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; request.ContentLength = postData.Length; // 提交请求数据 System.IO.Stream outputStream = request.GetRequestStream(); outputStream.Write(postData, 0, postData.Length); outputStream.Close(); HttpWebResponse response; Stream responseStream; StreamReader reader; string srcString; response = request.GetResponse() as HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("gb2312")); srcString = reader.ReadToEnd(); result = srcString; //返回值赋值 reader.Close(); if (!string.IsNullOrEmpty(result)) { try { XmlDocument M_XDoc = new XmlDocument(); M_XDoc.LoadXml(result); XmlElement eName; XmlNode M_Root = M_XDoc.SelectSingleNode("Root"); if (M_Root.Attributes["RetInt"].Value.Trim() == "0") result = "推送失败,失败原因:" + M_Root.Attributes["RetStr"].Value.Trim(); else result = "推送成功!"; } catch { result = "推送失败,解析失败,XML:" + result; } } else result = "推送失败,返回值为空!"; } catch (Exception ex) { result = "推送失败,失败原因:" + ex.Message; } return result; }
时间: 2024-11-13 03:54:39