C#开发WAP程序

类别:.NET开发 点击:0 评论:0 推荐:

平时下班等女朋友总是要等很长时间,想看书又没有带,只有玩手机游戏;GPRS上网,好象很多内容都是要付费的,就只有下载铃声,什么图片的,没劲!最好能用手机看看小说,网络上好象没有免费的WAP小说,办公室的小妹妹也想用手机看小说,看来只有自己做一个了。
都说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\" \"/r/n'>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\" \"/r/n'>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\" \"/r/n'>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#真好,写个小程序就这样简单

本文地址:http://com.8s8s.com/it/it42351.htm