//用A能得到维生素AD的正确结果,用中文则失败了
            string postData = "tym=平消片";
            //string postData = "tym=A";            byte[] data = Encoding.GetEncoding("GBK").GetBytes(postData);            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(@"http://222.66.164.74/xxcx/ypbm.jsp?lm=7");
            myRequest.Method = "POST";
            myRequest.Referer = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 (.NET CLR 3.5.21022)";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();            HttpWebResponse wmvRes = (HttpWebResponse)myRequest.GetResponse();
            StreamReader wmvRead = new StreamReader(wmvRes.GetResponseStream(), System.Text.Encoding.Default);
            StreamWriter wmvWrite = new StreamWriter(Application.StartupPath + @"\xxx.htm");
            wmvWrite.Write(wmvRead.ReadToEnd());
            wmvWrite.Flush();
            wmvWrite.Close();
            wmvRead.Close();

解决方案 »

  1.   

    不是编码问题是不是因为一个汉字是两个字节的问题
    byte[] data = Encoding.GetEncoding("GBK").GetBytes(postData); 
    和  myRequest.ContentLength = data.Length;             Stream newStream = myRequest.GetRequestStream(); 
                newStream.Write(data, 0, data.Length); 是不是有冲突??
     我也是猜的 我又一次是因为汉字是两个字节的问题错了 不知道你是不是
      

  2.   

    Encoding myEncoding = Encoding.GetEncoding("gb2312");
    byte[] postBytes = Encoding.ASCII.GetBytes(postData);HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(postUrl);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
    req.ContentLength = postBytes.Length;
      

  3.   

    编码问题
    Encoding.GetEncoding("GBK").GetBytes(postData); 
    换成 System.Text.Encoding.UTF8.GetBytes
    测试可用
      

  4.   


    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("some.abc.url");
    WebResponse resp = wr.GetResponse();
    string contentType = resp.ContentType.ToLower();
    int i = contentType.IndexOf("charset");接下来知道怎么做了吧
      

  5.   

    编码和解码问题,常见存在编码解码问题的情况:
    读写文件
    读写数据库

    文件或数据库中,字符串是以字节码的形式存储的。
    运行中的程序(.Net)的默认编码方式是Unicode
    我们经常看到的html文件中 charset=utf-8 是在解析html文件时的编码方式
    我们在另存文件时可以把字符串以某种编码方式(如Utf-8)解码成字节码
    所以,另存文件时必须和charset一致(在VS中自动按照charset的编码方式保存文件)。。
    我们在把文件读到程序中时,必须以文件的编码方式去把字节码编码成字符串。编码和解码
    编码:字节码---->字符串
    解码:字符串---->字节码
    如果理解了所有的编码解码问题就迎刃而解
      

  6.   

    byte[] data = Encoding.Default.GetBytes(postData);
    试试吧~
      

  7.   

    高手给的答案: 
    string postData = "tym=" + System.Web.HttpUtility.UrlEncode(Tym.Trim(), Encoding.GetEncoding("GB2312")) + "&sccj=" + System.Web.HttpUtility.UrlEncode(Sccj.Trim(), Encoding.GetEncoding("GB2312")) + "&sPagenum="+i.ToString();
    ......
     myRequest.ContentLength = postData.Length;                byte[] data = Encoding.UTF8.GetBytes(postData);
                    Stream newStream = myRequest.GetRequestStream();
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();