Lonz's 网络文档集

C#开发WAP程序



//设置ContentType类型
Response.ContentType="text/vnd.wap.wml";
//设置编码
Response.ContentEncoding=System.Text.Encoding.UTF8;
//以下是根据WML协议写的内容部份
WapContent="<?xml version=\"1.0\" encoding=\"UTF8\"?>\r\n";
WapContent+="<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\"><wml>\r\n";
WapContent+="\r\n";
WapContent+="<card id=\"hello\">\r\n";
WapContent+=" <p>hello老婆 </p>\r\n";
WapContent+="</card>\r\n";
WapContent+="\r\n";
WapContent+="</wml>\r\n";
WapContent+="\r\n";
Response.Write(WapContent);
用手机试一下,OK
现在写小说的WAP程序。
先从网上下一篇小说,存在目录下,1.txt
不过小说太大了,必须先要有一个目录,按2K分页吧,好家伙,有200页,算了,来一个直接选页数的GO吧:
WapContent+=" <input name=\"PAGENUM\" size=\"3\" maxlength=\"10\" type=\"text\" format=\"*N\" emptyok=\"true\"/>";
WapContent+="<anchor title=\"login\">\n";
WapContent+="<go href=\"wap.aspx\" method=\"get\">\n";
WapContent+="<postfield name=\"page\" value=\"$PAGENUM\"/>\n";
WapContent+="</go>就看这页!<br/>\n";
WapContent+="</anchor>\n";
所有的代码:
private void Page_Load(object sender, System.EventArgs e)
{
string WapContent;
string mC;
//在调试时用GB2312模拟器
Response.ContentType="text/vnd.wap.wml";
// Response.ContentType="text/vnd.wap.wml;charset=UTF-8";
Response.ContentEncoding=System.Text.Encoding.UTF8;// Unicode ;// "UTF8";
// Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
int n=0;
if(Request["Page"]==null)
{
//显示目录
WapContent="<?xml version=\"1.0\" encoding=\"UTF8\"?>\r\n";
// WapContent="<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n";
WapContent+="<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\"><wml>\r\n";

WapContent+="\r\n";
System.IO.FileInfo oFM=new System.IO.FileInfo(Request.PhysicalPath);
System.IO.StreamReader oReaderD=new System.IO.StreamReader(oFM.DirectoryName +"\\"+ "1.txt",System.Text.Encoding.Default);
//每次读3000字

mC=oReaderD.ReadToEnd() ;
oReaderD.Close();
WapContent+="<card id=\"lists"+n.ToString()+"\">\r\n";

int k=mC.Length/3000;
WapContent+=" <p>目录</p>\r\n";
WapContent+=" <p>共:"+k.ToString()+"页</p>\r\n";

WapContent+=" <input name=\"SID\" size=\"3\" maxlength=\"10\" type=\"text\" format=\"*N\" emptyok=\"true\"/>";
WapContent+="<anchor title=\"login\">\n";
WapContent+="<go href=\"wap.aspx\" method=\"get\">\n";
WapContent+="<postfield name=\"page\" value=\"$SID\"/>\n";
WapContent+="</go>就看这页!<br/>\n";
WapContent+="</anchor>\n";

WapContent+="<onevent type=\"onenterbackward\">\r\n";
WapContent+=" <prev/>\r\n";
WapContent+=" </onevent>\r\n";



WapContent+="</card>\r\n";



WapContent+="\r\n";



WapContent+="</wml>\r\n";
WapContent+="\r\n";
Response.Write(WapContent);
return;

}
else
n=System.Convert.ToInt32(Request["Page"]);


WapContent="<?xml version=\"1.0\"?>\r\n";
WapContent+="<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\"><wml>\r\n";

WapContent+="\r\n";
System.IO.FileInfo oF=new System.IO.FileInfo(Request.PhysicalPath);
System.IO.StreamReader oReader=new System.IO.StreamReader(oF.DirectoryName +"\\"+ "1.txt",System.Text.Encoding.Default);
//每次讯读3000字

mC=oReader.ReadToEnd() ;
oReader.Close();
if(mC.Length>4000)
{
mC=mC.Substring((n)*3000,3000);
mC=mC.Replace("\r\n","<BR/>");
ArtMore=true;
}

//每屏3000字


WapContent+="<card id=\"lists"+n.ToString()+"\">\r\n";

WapContent+=" <do type=\"accept\" label=\""+(n-1).ToString()+"\">\r\n";
WapContent+=" <go href=\"wap.aspx?Page="+(n-1).ToString()+"\"/>\r\n";
WapContent+=" </do>\r\n";
WapContent+="<onevent type=\"onenterbackward\">\r\n";
WapContent+=" <prev/>\r\n";
WapContent+=" </onevent>\r\n";

WapContent+=" <p>\r\n";
WapContent+=n.ToString()+"P";

WapContent+=mC;


WapContent+=" </p>\r\n";

WapContent+=" <do type=\"accept\" label=\"下一页\">\r\n";
WapContent+=" <go href=\"wap.aspx?Page="+(n+1).ToString()+"\"/>\r\n";
WapContent+=" </do>\n";
//选取页面
WapContent+=" <input name=\"SID\" size=\"3\" maxlength=\"10\" type=\"text\" format=\"*N\" emptyok=\"true\"/>";
WapContent+="<anchor title=\"login\">\n";
WapContent+="<go href=\"wap.aspx\" method=\"get\">\n";
WapContent+="<postfield name=\"page\" value=\"$SID\"/>\n";
WapContent+="</go>就看这页!<br/>\n";
WapContent+="</anchor>\n";
WapContent+="</card>\r\n";



WapContent+="\r\n";



WapContent+="</wml>\r\n";
WapContent+="\r\n";
Response.Write(WapContent);




}





培植wap输出格式
在webconfig中加入如下培植
  <system.web>
 
    <browserCaps>
            <result type="System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            <use var="HTTP_USER_AGENT"/>
                    preferredRenderingType = "wml11"
                    preferredRenderingMime = "text/vnd.wap.wml"
                    preferredImageMime = "image/vnd.wap.wbmp"
 </browserCaps> 




  </system.web>

posted on 2006-05-25 14:33  Lonz's 网络文档集  阅读(1166)  评论(0编辑  收藏  举报

导航