首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#POST回来的数据中文出现乱码

2013-10-04 
C#POST返回的数据中文出现乱码blic static string RequestUrl(string strUrl, Dictionarystring, string

C#POST返回的数据中文出现乱码

blic static string RequestUrl(string strUrl, Dictionary<string, string> postData, ref CookieContainer objCookieContainer)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
            req.Method = "POST";
            req.KeepAlive = true;
            req.ContentType = "application/x-www-form-urlencoded";
            req.Referer = strUrl.Remove(strUrl.LastIndexOf("/"));//,,.Substring(;
            // req.Timeout = 10000;
            if (objCookieContainer == null)
                objCookieContainer = new CookieContainer();
            req.CookieContainer = objCookieContainer;
            req.ContentLength = 0;
            if (postData != null && postData.Count > 0)
            {
                StringBuilder objEncodedPostDatas = new StringBuilder();
                foreach (KeyValuePair<string, string> kv in postData)
                {
                    objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Key));
                    objEncodedPostDatas.Append("=");
                    objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Value));
                    objEncodedPostDatas.Append("&");
                }
                byte[] byteData = Encoding.UTF8.GetBytes(objEncodedPostDatas.ToString().TrimEnd('&'));
                req.ContentLength = byteData.Length;
                Stream reqStream = req.GetRequestStream();
                reqStream.Write(byteData, 0, byteData.Length);
                req.CookieContainer.GetCookies(req.RequestUri);
                // reqStream.Close();
            }
            string strResponse = "";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            objCookieContainer = req.CookieContainer;
            Stream resStream = res.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, Encoding.Default);//.UTF8))
            strResponse = sr.ReadToEnd();
            // res.Close();
            return strResponse;
        }
上面是我POST的方法  返回过来的数据中文出现乱码 求高手解决下 C#?POST
[解决办法]
全部都用Encoding.Default试试

热点排行