webBrowser post的乱码问题

项目:xx 之 手工派单

项目介绍: xx 有两个派单形式,一是自动派单,二是手工派单.自动派单已经有程序实现,因此,需要用c#实现手工派单模块,由于xx的派单流程是 从综合告警(c/s)传 整个告警对象的某些设置的属性 到emos(电子运维)系统(b/s),因此,在该模块中,用到了webbrowser控件.

程序处理流程介绍:

在程序中,首先读取一个配置文件的结点,查找到告警对象的某个属性,然后获取属性值(用反射),拼一个htm页面.该页面代码如下:

string s=

<! DOCTYPE html PUBLIC  ' -//W3C//DTD XHTML 1.0 Transitional//EN '   ' http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ' >
< html xmlns = ' http://www.w3.org/1999/xhtml ' >
< meta http - equiv = ' Content-Type '  content = ' text/html;charset=gb2312 ' >
< head >< title > 表单设置 </ title >
</ head >
< body id = ' CForwardDlg '  BGCOLOR = ' LIGHTGREY '  onload = ' onload(); ' >
< form method = ' post '  action = ' http://localhost/test.asp '  id = ' ID_AlarmForm '  name = ' ID_AlarmForm ' >
< input type = ' hidden '  name = ' oriAlarmId '  id = ' ID_oriAlarmId '  value = ' 2007-8-21 21:36:42 ' />
< input type = ' hidden '  name = ' alarmTitle '  id = ' ID_alarmTitle '  value = ' HLR北京 ' />
< input type = ' hidden '  name = ' alarmCreateTime '  id = ' ID_alarmCreateTime '  value = ' www ' />
< input type = ' hidden '  name = ' neType '  id = ' ID_neType '  value = ' beijing ' />
< input type = ' hidden '  name = ' neName '  id = ' ID_neName '  value = ' hubeisheng ' />
< input type = ' hidden '  name = ' alarmLevel '  id = ' ID_alarmLevel '  value = ' wuhanshi ' />
< input type = ' hidden '  name = ' alarmType '  id = ' ID_alarmType '  value = ' 12222 ' />
</ form >
< script language = ' javascript '  type = ' text/javascript ' >
function onload()
{
var tt
= document.getElementById( ' ID_AlarmForm ' );
if (tt != undefined)
{
tt.submit();
}
}
</ script >
</ body >
</ html >

 

然后,post接收页面:

 

< HTML >
< head >
< title > 工单手工前转 </ title >
< META HTTP - EQUIV = " Content-Type "  CONTENT = " text/html; charset=gb2312 " />
< script language = " JavaScript " >
<!--
function onload()
{   
    document.ID_AlarmForm.submit();
}

-->
</ script >
</ head >
< BODY BGCOLOR = LIGHTGREY >
< div  id = " DB " >
</ div >
< pre >
userName        :
<%   = request( " userName " %>
systemName      :
<%   = request( " systemName " %>
serSupplier     :
<%   = request( " serSupplier " %>
serCaller       :
<%   = request( " serCaller " %>
callTime        :
<%   = request( " callTime " %>
alarmID         :
<%   = request( " alarmID " %>
orialarmId      :
<%   = request( " oriAlarmId " %>
alarmTitle      :
<%   = request( " alarmTitle " %>
alarmCreateTime :
<%   = request( " alarmCreateTime " %>
neType          :
<%   = request( " neType " %>
neName          :
<%   = request( " neName " %>
alarmLevel      :
<%   = request( " alarmLevel " %>
alarmType       :
<%   = request( " alarmType " %>
alarmRedefLevel :
<%   = request( " alarmRedefLevel " %>
alarmRedefType  :
<%   = request( " alarmRedefType " %>
alarmLocation   :
<%   = request( " alarmLocation " %>
alarmDetail     :
<%   = request( " alarmDetail " %>
alarmPropose    :
<%   = request( " alarmPropose " %>
alarmRegion     :
<%   = request( " alarmRegion " %>
</ pre >
<!--  当派单成功或取消时模拟Click下面相应的BUTTON,ID保持不变 -->
< BUTTON STYLE = " WIDTH:30 "  ID = " ButtonOK " > 确定 </ BUTTON >
< BUTTON STYLE = " WIDTH:30 "  ID = " ButtonCancel " > 取消 </ BUTTON >

</ BODY >
</ HTML >

webbrowser绑定:

webBrowser.DocumentText=s;

最后,在页面上,webbrowser上显示的结果,最后接收页面,中文会出现乱码.而如果把这两处代码保存为htm 和asp页面后,浏览htm页面post后asp页面不会出现乱码.

解决办法:

程序中不拼写第一个页面,改为这样的:

strTemp = string.Format(@"{0}={1}&", s, System.Web.HttpUtility.UrlEncode(strValue, System.Text.Encoding.GetEncoding("gb2312")));
                    strBuilder.Append(strTemp);

既弄成象url地址一样的串.但是,主要的一点,还是 System.Web.HttpUtility.UrlEncode的转换.

最后,绑定到webbrowser控件:

 string headers = "Content-Type: application/x-www-form-urlencoded";
            this.webBrowser.Navigate(http://localhost/test.asp, "", System.Text.Encoding.GetEncoding("gb2312").GetBytes(s), headers);

 

读取webBrowser的源码

不能够直接用webBrowser.DocumentText读取,这样出来的是乱码

 System.IO.StreamReader streamReader = new System.IO.StreamReader(this.webBrowser.DocumentStream, System.Text.Encoding.GetEncoding("gb2312"));
            string strWebBrowserDocument = streamReader.ReadToEnd();

你可能感兴趣的:(WebBrowser)