乱码的问题,折腾两天了,还是解决不了,思归,孟子,秋枫等高人请进

linuxyf 2005-12-23 10:50:10
我用C#调用一个Servlet,Servlet中从header中取出的中文信息都是乱码,不管我怎么转换,都不行

C#代码如下:
try
{
System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes("");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8080/Biometrics/CheckPasswordServlet");
req.Method="POST";
req.Headers.Add("UserID","中国人");
req.Headers.Add("HospitalID", "1");
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream newStream=req.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
}catch(Exception ex)
{
textBox1.Text = ex.Message;
}

Servlet部分代码如下:
protected int doProcess(HttpServletRequest req, HttpServletResponse res)
throws BiometricsException {
String userID = null;
try {
userID = req.getHeader("UserID").trim();
logger.debug("UserID: " + userID);
userID = new String(userID.getBytes("iso-8859-1"), "GB2312");
logger.debug("UserID: " + userID);

日志中记录的userID都是乱码,这是为何?
...全文
325 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
linuxyf 2005-12-26
  • 打赏
  • 举报
回复
问题解决了,谢谢楼上的各位,尤其感谢思归兄。
marvelstack 2005-12-25
  • 打赏
  • 举报
回复
try
http://blog.csdn.net/goody9807/articles/138871.aspx
微量科技 2005-12-24
  • 打赏
  • 举报
回复
嘿嘿,顶,学习学习,看到两颗钻石,无比兴奋!
saucer 2005-12-23
  • 打赏
  • 举报
回复
you want to try the method in this article?

http://www.javaresearch.org/article/showarticle.jsp?column=545&thread=8588
saucer 2005-12-23
  • 打赏
  • 举报
回复
try to use java.net.URLDecoder.decode

java.net.URLDecoder.decode("encoded string","the matching encoding here")
cxbkkk 2005-12-23
  • 打赏
  • 举报
回复
utf-8比较方便,有时候textbox输入的东西读出来的是乱码
linuxyf 2005-12-23
  • 打赏
  • 举报
回复
saucer(思归)兄,我用你提供的方法,怎么接收到的还是乱码阿,晕了
zihu928 2005-12-23
  • 打赏
  • 举报
回复
hoho,向思归大哥学习……
jxufewbt 2005-12-23
  • 打赏
  • 举报
回复
saucer(思归) 老大正解
saucer 2005-12-23
  • 打赏
  • 举报
回复
what if you try


req.Headers.Add("UserID",HttpUtility.UrlEncode("中国人", System.Text.Encoding.GetEncoding("GB2312")));

or

req.Headers.Add("UserID",HttpUtility.UrlEncode("中国人", System.Text.Encoding.UTF8));
zihu928 2005-12-23
  • 打赏
  • 举报
回复
乱码问题,最好用utf-8来编码,最安全
linuxyf 2005-12-23
  • 打赏
  • 举报
回复
saucer(思归)兄,如果我在客户端C#中使用HttpUtility.UrlEncode进行编码了,
那我在Servlet中如何解吗?

req.Headers.Add("UserID",HttpUtility.UrlEncode("中国人", System.Text.Encoding.GetEncoding("GB2312")));

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧