我用HttpWebRequest发送Web请示到一个地址,其中有一个参数的内容是中文,为什么在发送的时候中文就已经变成乱码了呀?
是不是在发送之前要对中文编码?我试过urlencode,但是没有效果,高手帮帮忙啊!谢谢啊!!1

解决方案 »

  1.   

    http://time-is-life.cnblogs.com/articles/331969.html
      

  2.   

    String strMsg = "人民剧场:这是一条测试用数据,请在完成测试后删除本条记录!本测试";
    strParam = "http://localhost/msg/recv.asp?val=" + strMsg;
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strParam);
    //myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
    myHttpWebRequest.Accept = "zh-cn";
    myHttpWebRequest.ContentType = "text/html;charset=gb2312"; 
    myHttpWebRequest.Method = "GET";
    myHttpWebRequest.CookieContainer = cc;
    myHttpWebRequest.Timeout = 10000;
    myHttpWebRequest.CookieContainer = cc;
    myHttpWebRequest.Timeout = 10000;
    HttpWebResponse resp = myHttpWebRequest.GetResponse() as HttpWebResponse;
    Stream s = resp.GetResponseStream();
    if(s.CanRead==true)
    {
    StreamReader sr = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));   
    strText = sr.ReadToEnd();
    sr.Close();
    }
    resp.Close();strText 是返回的值,这个值就是请示时发送的val参数,再次得到该值时就成为乱码,我觉得应该是在发送的时候就成乱码的,是不是要把请示做一下编码什么的?
      

  3.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=ATV1GLXT-65FF-4M82-CT5U-B1J65D3ZN2OK
      

  4.   

    把web.config里面的编码和你的页面中的编码都设置为gb2312
      

  5.   

    StringBuilder UrlEncoded = new StringBuilder();
    Char[] reserved = {'?', '=', '&'};
    byte[] SomeBytes = null; int i=0, j;
    while(i<strMsg.Length)
    {
    j=strMsg.IndexOfAny(reserved, i);
    if (j==-1)
    {
    UrlEncoded.Append(HttpUtility.UrlEncode(strMsg.Substring(i, strMsg.Length-i)));
    break;
    }
    UrlEncoded.Append(HttpUtility.UrlEncode(strMsg.Substring(i, j-i)));
    UrlEncoded.Append(strMsg.Substring(j,1));
    i = j+1;
    } myHttpWebRequest.ContentLength=strMsg.Length;  Stream myHttpWebRequestStream=myHttpWebRequest.GetRequestStream(); 
    StreamWriter myStreamWriter=new StreamWriter(myHttpWebRequestStream,Encoding.GetEncoding("gb2312"));                 
    myStreamWriter.Write(strMsg); 
    myStreamWriter.Close(); 
    myHttpWebRequestStream.Close();
    **********************************
    *本人主要使用VB+MS SQL,C#略知一二
    *                                
    *如有相关问题需要帮助            
    *                                
    *可发短消息告知链接    
    *
    *助人为快乐之本!         
    **********************************
      

  6.   

    在客户端发送到Server端的时候用escape()将中文参数编码,在Sever端转到另一个页面的话,直接用中文就行了