日期:2014-05-17  浏览次数:20925 次

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);