技术文章 > C#开发WAP程序实例

C#开发WAP程序实例

2018-11-17 14:17

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:

平时下班等女朋友总是要等很长时间,想看书又没有带,只有玩手机游戏;GPRS上网,很多内容都是需要付费的,并且就只有可怜的下载铃声,图片,好象还有新新,没劲!最好能用手机看看小说,网络上好象没有免费的WAP小说,办公室的小妹妹也是无聊之至,老想买个PDA看小说,又方便自己,又方便美眉,看来还是DIY吧。
都说JAVA是开发WAP的利器,不过WAP需要这个“码”转到那个“码”,有好多的东东需要学,我可是最懒的人!算了,看一下能不能用C#做一个WAP小站。
想来也很简单的,WAP就是用UTF8编码嘛,好象还有什么WML之类的,WML在网上查查资料,就和XML差不多,平时用XML也蛮多的,WML也就简单了,C#直接可以设RESPONSE的编码的,看来有戏了,先写一个“Hello 妹妹“吧,

//设置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);
}
拿起手机,www.tjkx.com/wap.aspx,试一下,爽!!!
用手机调试太麻烦,下载一个WinWap Pro3.1在这里面调试。
工具齐备了,C#真好,写个小程序就这样简单