当前位置:编程学习 > C#/ASP.NET >>

HttpWebRequest 读取网站返回为中文成乱码请指导员

        public string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
        {
            if (string.IsNullOrEmpty(postData))
            {
                return GetHtml(url, cookieContainer);
            }

            Thread.Sleep(delay);
            currentTry++;
            try
            {
                byte[] byteRequest = Encoding.Default.GetBytes(postData);

                HttpWebRequest httpWebRequest;
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = isPost ? "POST" : "GET";
                httpWebRequest.ContentLength = byteRequest.Length;
                encode = Encoding.GetEncoding("GB2312");
                Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();

                HttpWebResponse httpWebResponse;
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();

                currentTry = 0;
                return html;
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
                Console.ForegroundColor = ConsoleColor.White;

                if (currentTry <= maxTry)
                {
                    GetHtml(url, postData, isPost, cookieContainer);
                }

                currentTry = 0;
                return string.Empty;
            }
        }

public static string Login(string loginEmail, string loginPassword)
        {
            string loginUrl = "http://www.222idc.com/user/userlogin.asp";
            string postData = string.Format("username={0}&password={1}", loginEmail, loginPassword);
            string result = httpHelper.GetHtml(loginUrl, postData, true, cookieContainer);
            return result;
        }


HttpWebRequest 读取网站返回为中文成乱码请指导员  上以是我使用的代码,还是一个登录加cookieS记录的.

Login为登录事件,可是返回result里的中文全是乱码了.请大虾修改指导员.

--------------------编程问答-------------------- 用Fiddler抓包看看,有可能返回的是 Gzip 压缩流。 --------------------编程问答-------------------- 把StreamReader streamReader = new StreamReader(responseStream, encoding);这句的编码换成utf-8试试,如不行,就是如1楼所说的那样。
--------------------编程问答-------------------- 问题解决,非常感谢楼上二位,是我的自己粗心了.问题出在encoding这个变量上.我设置错了. --------------------编程问答-------------------- 最近也在研究这个东西,想用WEB的去保存别的网站的登录操作怎么做?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,