本地测试通过,服务器发布出现乱码 ,后面检查发现Http请求发送存在编码问题
改进了一下请求基类:
using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Net; using System.IO; using System.IO.Compression; using System.Text.RegularExpressions; namespace common { public class HttpUtil { public string HttpPost(string url, string data, Encoding encoding) { return HttpPost(url, data, "text/plain", encoding); } public string HttpGet(string url, Encoding encoding) { return HttpSend(url, "", "text/plain", Encoding.GetEncoding("utf-8"), "GET"); } public string HttpGet(string url, Encoding encoding, string Strcompress) { return HttpSend(url, "", "text/plain", Encoding.GetEncoding("utf-8"), "GET", null, Strcompress); } public string HttpGet(string url, string contenttype, Encoding encoding) { return HttpSend(url, "", contenttype, Encoding.GetEncoding("utf-8"), "GET"); } public string HttpPost(string url, string data, string contenttype, Encoding encoding, Dictionary<string, string> Headers = null) { return HttpSend(url, data, contenttype, encoding, "POST", Headers); } private string HttpSend(string url, string data, string contenttype, Encoding encoding, string method, Dictionary<string, string> Headers = null, string Strcompress = "") { try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = method; if (!string.IsNullOrEmpty(contenttype)) request.ContentType = contenttype; //设置Header信息 if (Headers != null && Headers.Count > 0) { foreach (KeyValuePair<string, string> item in Headers) if (!string.IsNullOrEmpty(item.Key) && !string.IsNullOrEmpty(item.Value)) request.Headers.Add(item.Key, item.Value); } if (data != "") { byte[] bs = encoding.GetBytes(data); request.ContentLength = bs.Length; request.Timeout = 120 * 1000; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); if (Strcompress == "gzip") stream = new GZipStream(stream, CompressionMode.Decompress); //if (response.ContentEncoding.ToLower().Contains("gzip")) // stream = new GZipStream(stream, CompressionMode.Decompress); //else if (response.ContentEncoding.ToLower().Contains("deflate")) // stream = new DeflateStream(stream, CompressionMode.Decompress); MemoryStream memoryStream = new MemoryStream(); int bufferLength = 1024; byte[] b = new byte[bufferLength]; int actual; while ((actual = stream.Read(b, 0, bufferLength)) > 0) { memoryStream.Write(b, 0, actual); } memoryStream.Position = 0; byte[] bt = memoryStream.ToArray(); //无视编码 //if (encoding == null) //{ string temp = Encoding.Default.GetString(bt, 0, bt.Length); //<meta(.*?)charset([\s]?)=[^>](.*?)> Match meta = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"‘]", RegexOptions.IgnoreCase | RegexOptions.Multiline); string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty; charter = charter.Replace("\"", string.Empty).Replace("‘", string.Empty).Replace(";", string.Empty); if (charter.Length > 0) { charter = charter.ToLower().Replace("iso-8859-1", "gbk"); encoding = Encoding.GetEncoding(charter); } else { if (response.CharacterSet.ToLower().Trim() == "iso-8859-1") { encoding = Encoding.GetEncoding("gbk"); } else { if (string.IsNullOrEmpty(response.CharacterSet.Trim())) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } //} stream.Close(); stream.Dispose(); response.Close(); return encoding.GetString(bt); } catch (Exception e) { LogUtils.Debug(e.Message); return ""; } } /// <summary> /// 以指定编码通过POST方式发送数据。 /// </summary> /// <param name="Url">url</param> /// <param name="postString">Post数据</param> /// <returns></returns> public string PostString(string Url, string postString, Encoding ed) { var data = ed.GetBytes(postString); var request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; using (var wStream = request.GetRequestStream()) { wStream.Write(data, 0, data.Length); } var response = (HttpWebResponse)request.GetResponse(); var rt = string.Empty; using (var rStream = new StreamReader(response.GetResponseStream(), ed)) { rt = rStream.ReadToEnd(); } response.Close(); return rt; } /// <summary> /// 以utf-8编码通过POST方式发送数据。 /// </summary> /// <param name="Url">url</param> /// <param name="postString">Post数据</param> /// <returns></returns> public string PostString_UTF8(string Url, string postString) { return PostString(Url, postString, Encoding.UTF8); } } }
时间: 2024-11-06 20:12:25