HttpListner通过url传入中文参数就是乱码 在线等

Persistence_x 2013-07-25 10:19:00
1.winform程序通过输入网址http://localhost:8080/web/?USERID=221122&MOBILE_NO=1500166624,1342655556&SMS_CONTENT=2322中文
在程序里 string SMS_CONTENT = hlc.Request.QueryString[canshu4];拿到这个值的话 是2322乱码
我如何才能正确的拿到2322中文这个值 请大神们帮忙解决下
try
{
//创建HttpListener对象
using (HttpListener hlistener = new HttpListener())
{
//获取设置用户端身份验证
hlistener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
//设置url前缀$
//hlistener.Prefixes.Add("http://localhost:8080/web/");
//通过IP来访问
//获取app.config文件的url地址
string HttpUrl = System.Configuration.ConfigurationManager.AppSettings["HttpURL"].ToString();

hlistener.Prefixes.Add(HttpUrl);
//接收传入请求
hlistener.Start();
//创建connection对象
SqlConnection conn = new SqlConnection("server=.;database=StuTest;uid=sa;pwd=xdxadmin");
while (true)
{

//等待Http传入请求
HttpListenerContext hlc = hlistener.GetContext();
//设置返回客户端http代码
hlc.Response.StatusCode = 200;

//读取app参数
string canshu1 = System.Configuration.ConfigurationManager.AppSettings["Usercanshu"].ToString();
string canshu3 = System.Configuration.ConfigurationManager.AppSettings["mobcanshu"].ToString();
string canshu4 = System.Configuration.ConfigurationManager.AppSettings["smscanshu"].ToString();
//获取url的值
string USERID = hlc.Request.QueryString[canshu1];
string MOBILE_NO = hlc.Request.QueryString[canshu3];
string SMS_CONTENT = hlc.Request.QueryString[canshu4];
int LCONTENT = SMS_CONTENT.Length;

//开始进行判断
var q = MOBILE_NO.Split(',').ToList();
if (q != null)
{
foreach (var q1 in q)
{
//插入到数据库
SqlCommand comm = new SqlCommand(string.Format("insert into SMS_MT([GUID],[USERID],NEED_DOWN_TIME,FACT_DOWN_TIME,MOBILE_NO,SMS_CLENGTH,SMS_CONTENT) values(newid(),'" + USERID + "',getdate(),dateadd(second,2,getdate()),'" + q1 + "','" + LCONTENT + "','" + SMS_CONTENT + "')"), conn);
conn.Open();
comm.ExecuteNonQuery();

}
}
log.Info(DateTime.Now.ToString() + ": login success");//写入一条新log
log.Error(DateTime.Now.ToString() + ": login Error");//写入一条新log
using (StreamWriter sw = new StreamWriter(hlc.Response.OutputStream))
{
sw.WriteLine(USERID);
//sw.WriteLine(pw);
sw.WriteLine(MOBILE_NO);
sw.WriteLine(SMS_CONTENT);
sw.Close();
hlc.Response.Close();
}
}
}
}
catch (Exception exc)
{
log.Info(exc.Message+":"+exc.StackTrace);
log.Error(exc.Message + ":" + exc.StackTrace);
throw exc;
}

}

下面是app.config
<?xml version="1.0"  ?>


<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>

<appSettings>
<add key="HttpURL" value="http://localhost:8080/web/"/>
<add key="Usercanshu" value="USERID"/>
<add key="mobcanshu" value="MOBILE_NO"/>
<add key="smscanshu" value="SMS_CONTENT"/>
</appSettings>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

<param name="File" value="Log\Log.txt"/>

<param name="AppendToFile" value="true"/>

<param name="MaxSizeRollBackups" value="100"/>

<param name="MaximumFileSize" value="2MB"/>

<param name="RollingStyle" value="Size"/>

<param name="StaticLogFileName" value="true"/>

<layout type="log4net.Layout.PatternLayout">

<param name="ConversionPattern" value="%-5p %d [%c] %m%n"/>

</layout>

</appender>

<root>

<level value="all"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
...全文
560 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Persistence_x 2013-07-29
  • 打赏
  • 举报
回复 1
问题已经解决! 使用request.Rawurl拿到值string filename =Path.GetFileName(hlc.Request.RawUrl); 使用HttpUtility.ParseQueryString(filename).Get(canshu4)截取参数
Persistence_x 2013-07-26
  • 打赏
  • 举报
回复
引用 8 楼 jingcairensheng 的回复:
USERID=221122&MOBILE_NO=1500166624,1342655556&SMS_CONTENT=" + System.Web.HttpUtility.UrlEncode("2322中文",Encoding.UTF8); string SMS_CONTENT = System.Web.HttpUtility.UrlDecode(hlc.Request.QueryString[canshu4],Encoding.UTF8); 你看看整体用的是什么编码,在编码的时候就用什么编码,统一下试试
你说的怎么给他加编码,我通过url地址过来的 只要用hlc.request.querystring[canshu4]就是乱码了
Persistence_x 2013-07-26
  • 打赏
  • 举报
回复
引用 6 楼 jingcairensheng 的回复:
能不能将canshu4对应的值保存时先编码使用时后解码
你说的意思是HttpUtility.UrlEncode 然后HttpUtility.UrlDecode吗 这样过来也是不行的
jingcairensheng 2013-07-26
  • 打赏
  • 举报
回复
USERID=221122&MOBILE_NO=1500166624,1342655556&SMS_CONTENT=" + System.Web.HttpUtility.UrlEncode("2322中文",Encoding.UTF8); string SMS_CONTENT = System.Web.HttpUtility.UrlDecode(hlc.Request.QueryString[canshu4],Encoding.UTF8); 你看看整体用的是什么编码,在编码的时候就用什么编码,统一下试试
Persistence_x 2013-07-26
  • 打赏
  • 举报
回复
引用 5 楼 dongxinxi 的回复:
url = "http://localhost:8080/web/?USERID=221122&MOBILE_NO=1500166624,1342655556&SMS_CONTENT=" + System.Web.HttpUtility.UrlEncode("2322中文"); string SMS_CONTENT = System.Web.HttpUtility.UrlDecode(hlc.Request.QueryString[canshu4]);
这次换取过来还是乱码.请在帮忙看看
jingcairensheng 2013-07-26
  • 打赏
  • 举报
回复
能不能将canshu4对应的值保存时先编码使用时后解码
  • 打赏
  • 举报
回复
url = "http://localhost:8080/web/?USERID=221122&MOBILE_NO=1500166624,1342655556&SMS_CONTENT=" + System.Web.HttpUtility.UrlEncode("2322中文"); string SMS_CONTENT = System.Web.HttpUtility.UrlDecode(hlc.Request.QueryString[canshu4]);
Persistence_x 2013-07-26
  • 打赏
  • 举报
回复
引用 3 楼 jingcairensheng 的回复:
using (StreamWriter sw = new StreamWriter(hlc.Response.OutputStream,Encoding.UTF8)) { sw.WriteLine(USERID); //sw.WriteLine(pw); sw.WriteLine(MOBILE_NO); sw.WriteLine(SMS_CONTENT); sw.Close(); hlc.Response.Close(); }
你好 现在是这句话插入的时候就是乱码 string SMS_CONTENT =hlc.Request.QueryString[canshu4].ToString() 传的时候是2322中得文 进来就是2322脰脨碌脙脦脛
jingcairensheng 2013-07-26
  • 打赏
  • 举报
回复
using (StreamWriter sw = new StreamWriter(hlc.Response.OutputStream,Encoding.UTF8)) { sw.WriteLine(USERID); //sw.WriteLine(pw); sw.WriteLine(MOBILE_NO); sw.WriteLine(SMS_CONTENT); sw.Close(); hlc.Response.Close(); }
jingcairensheng 2013-07-26
  • 打赏
  • 举报
回复
StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
周美文 2013-07-26
  • 打赏
  • 举报
回复
指定编码,Defalut就好
jingcairensheng 2013-07-26
  • 打赏
  • 举报
回复
utf8换成GB2312,试试
Persistence_x 2013-07-25
  • 打赏
  • 举报
回复
传入的http://localhost:8080/web/?USERID=221122&MOBILE_NO=1500166624,1342655556&SMS_CONTENT=2322中得文 得到的SMS_CONTENT等于2322脰脨碌脙脦脛

110,538

社区成员

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

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

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