手机与servlet的网络通信技巧.htm

来自「随着越来越多手提电话和个人数字助理开始融入到信息高速公路之上」· HTM 代码 · 共 616 行 · 第 1/5 页

HTM
616
字号
                  style="mso-tab-count: 2">                   
                  </SPAN>try{<O:P> </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN>hc = 
                  (HttpConnection)Connector.open(url,Connector.READ_WRITE);<O:P> 
                  </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN>//</SPAN><SPAN 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN; mso-fareast-font-family: 'MS Sans Serif'">设置请求属性</SPAN><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><O:P> 
                  </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;</SPAN>hc.setRequestMethod(HttpConnection.POST);<SPAN 
                  style="mso-spacerun: yes">&nbsp; </SPAN>//</SPAN><SPAN 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN; mso-fareast-font-family: 'MS Sans Serif'">设置为</SPAN><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">POST</SPAN><SPAN 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN; mso-fareast-font-family: 'MS Sans Serif'">请求方式</SPAN><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">,默认的请求方式是GET<O:P> 
                  </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
                  style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN>hc.setRequestProperty("IF-Modified-Since","15 Oct 2003 
                  08:47:14 GMT");<O:P> </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
                  style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN>hc.setRequestProperty("User-Agent","Profile/MIDP-1.0 
                  Configuration/CLDC-1.0");<O:P> </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
                  style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN>hc.setRequestProperty("Content-Language","en-CA");<O:P> 
                  </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>hc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");<O:P> 
                  </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN><SPAN 
                  style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>hc.setRequestProperty("Connection","Keep-Alive");//</SPAN><SPAN 
                  lang=EN-US 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">Connection</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">头可以控制</SPAN><SPAN 
                  lang=EN-US 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">MIDlet</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">和</SPAN><SPAN 
                  lang=EN-US 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">Web</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">服务器之间保持</SPAN><SPAN 
                  lang=EN-US 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">"keep 
                  alive"特色。"keep alive"</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">特色是指在</SPAN><SPAN 
                  lang=EN-US 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">MIDlet</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">和</SPAN><SPAN 
                  lang=EN-US 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">Web</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">服务器间始终使用同一个</SPAN><SPAN 
                  lang=EN-US 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">HTTP</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">连接来多次传递数据</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">(</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">在通常情况下</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">,<SPAN 
                  lang=EN-US>HTTP</SPAN></SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">是无连接的协议</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">,</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">每次数据传输完毕后都将断开连接</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">,</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">而下次传递数据之前将重新建立连接</SPAN><SPAN 
                  style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">)</SPAN><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"> 
                  <O:P></O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  </SPAN>//</SPAN><SPAN 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN; mso-fareast-font-family: 'MS Sans Serif'">发送请求参数到servlet</SPAN><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><O:P> 
                  </O:P></SPAN></P>
                  <P align=left class=MsoNormal 
                  style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN 
                  lang=EN-US 
                  style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN 
                  style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?