2011-08-08 66 views
1

我想使用Android Java「模擬」瀏覽器併發布登錄數據。我想這How to submit HTML form data (method=POST) using JAVA 但我失敗了.....如何從android發佈表單數據?

的URL:http://192.168.254.34/Control ID = 1000

這裏是html源代碼:

<html><head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title>歡迎使用WLAN無線寬帶</title> 
<style><!-- 
a {text-decoration: none} 
.f1 { font-size: 10.5pt; font-family: Verdana; line-height: 12pt } 
.f2 { font-size: 8pt; font-family: Verdana; line-height: 12pt } 
.btn { 
BORDER-LEFT: #7b9ebd 1px solid; 
BORDER-RIGHT: #7b9ebd 1px solid; 
BORDER-TOP: #7b9ebd 1px solid; 
BORDER-BOTTOM: #7b9ebd 1px solid; 
PADDING-LEFT: 2px; 
PADDING-RIGHT: 2px; 
PADDING-TOP: 2px; 
FONT-SIZE: 10.5pt; 
CURSOR: hand; 
COLOR: black; 
FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#cecfde); 
} 
--> 
</style> 
<base href="http://192.168.254.34/pageset/131/PDA/"> 
</head><body topmargin="0" leftmargin="0"> 
       <!-- 本地動態密碼開始--> 
       <script> 
        function submitform2(){ 
         if (form1.strAccount.value==""){ 
          alert("請填入用戶名!"); 
          return; 
         } 
         form2.num.value=form1.strAccount.value; 
         form2.submit(); 
        } 
       </script> 
       <iframe scr="about:blank" width="0" height="0" name="a"></iframe> 
       <FORM name="form2" method=post action="http://218.204.223.213:8080/cmpp/service.do?id=newuser" target="a"> 
        <input size=12 type=hidden name="num" maxlength=26 style="border:1px solid #999999; FONT-SIZE: 10.5pt; BACKGROUND: #ffffff; COLOR: #333399; FONT-FAMILY: verdana; 1px:" size="14"></td> 
        <INPUT value="172.31.18.199" type=hidden name=ip> 
       </FORM> 

      <!-- 本地動態密碼結束 --><form name=form1 method=post action="/Control?id=2000"> 

<center> 
<table width=600 border=0 height=800> 
    <tr> 
    <td height=800> 
    <div align=center> 
    <center> 
    <table width=600 border=0 height=800 bgcolor=#FFFFFF cellspacing=0 cellpadding=0 style="border-collapse: collapse" bordercolor=#111111> 
    <tr> 
     <td height=800 width=600 background="a02.jpg" align="center"> 
     <table border="0" width="288" cellspacing="0" cellpadding="0" height="97%"> 
      <tr> 
       <td height="82" colspan="2"> 
       <p align="center" class="f1"> 
        </td> 
      </tr> 
      <tr> 
       <td height="295" align="center" colspan="2"> 
       <table border="0" width="104%" cellspacing="0" cellpadding="0" height="106%"> 
        <tr> 
         <td height="148" colspan="2"> 
         <p align="center"> 
         <font color="#333399" style="font-size: 10.5pt"> 
         歡迎使用WLAN無線登陸頁面</font></td> 
        </tr> 
        <tr> 
         <td width="32%" height="33" align="right" class="f1"> 
         用戶帳號</td> 
         <td height="33" width="68%"> 
         &nbsp;<input name=strAccount maxlength=26 style="border:1px solid #999999; FONT-SIZE: 10.5pt; BACKGROUND: #ffffff; COLOR: #333399; FONT-FAMILY: verdana; 1px:" size="21"></td> 
        </tr> 
        <tr> 
         <td width="32%" align="right" class="f1">用戶密碼</td> 
         <td width="68%"> 
         &nbsp;<input type=password name=strPassword maxlength=16 style="border:1px solid #999999; FONT-SIZE: 10.5pt; BACKGROUND: #ffffff; COLOR: #333399; FONT-FAMILY: verdana; 1px:"   size="21"></td> 
        </tr> 
        <tr> 
         <td height="96" colspan="2" align="left"> 
         <p align="center"> 
        &nbsp;<input type="button" value="登 錄" width="86" height="23" name="I1" onclick="javascript:form1.submit();" class="btn">&nbsp;&nbsp; 
        <input type="reset" value="重 置" width="86" height="23" name="I2" class="btn"></td> 
        </tr> 
       </table> 
       </td> 
      </tr> 
      <tr> 
       <td class="f1" width="255"> 
       <p align="center"> </td> 
       <td class="f1" width="33"> 
        </td> 
      </tr> 
      <tr> 
       <td class="f1" height="91" width="255"> 
       <p align="center" class="f1"> 
        </td> 
       <td class="f1" height="91" width="33"> 
        </td> 
      </tr> 
      <tr> 
       <td height="108" colspan="2"> 
       <p align="center"> 
       &nbsp;<br> 
       </td> 
      </tr> 
     </table> 
     </td> 
    </tr> 
    </table></center></div></td></tr></table></center></form></body></html> 

的頭,我得到:(使用鉻)

Request URL:http://192.168.254.34/Control?id=2000 
Request Method:POST 
Status Code:200 OK 
Request Headersview source 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Cache-Control:max-age=0 
Connection:keep-alive 
Content-Length:28 
Content-Type:application/x-www-form-urlencoded 
Cookie:JSESSIONID=45129DDCC372B780F65344DB9DB107FB 
Host:192.168.254.34 
Origin:http://192.168.254.34 
Referer:http://192.168.254.34/Control?id=1000 
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko)    Chrome/13.0.782.107 Safari/535.1 
Query String Parametersview URL encoded 
id:2000 
Form Dataview URL encoded 
strAccount:username 
strPassword:password 
Response Headersview source 
Cache-Control:no-cache 
Content-Length:2202 
Content-Type:text/html;charset=GBK 
Date:Mon, 08 Aug 2011 11:04:25 GMT 
Expires:Thu, 01 Jan 1970 00:00:00 GMT 
Pragma:No-cache 
Server:Apache-Coyote/1.1 
Set-Cookie:intPageSetID=131; Expires=Mon, 08-Aug-2011 11:04:55 GMT 

有人知道如何登錄後數據?????????

+0

可能重複的[提交形式與Android應用POST數據](http://stackoverflow.com/questions/2999945/submit-form-with-post-data-in-android-app) –

回答

0

使用HttpClient。我可以做這個帖子並處理認證。文件here

+0

設我試試........... – user883883

3

這裏是我如何做到這一點:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("www.server.com/dologin.php"); 

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("username", "KennyStarfighter"); 
nameValuePairs.add(new BasicNameValuePair("password", "SecretPassword"); 
nameValuePairs.add(new BasicNameValuePair("redirect", "?controller=login&view=loggedonpage")); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity he = response.getEntity(); 

然後我就可以例如處理響應爲XML。的

xmlReader.parse(new InputSource(he.getContent())); 
+0

它不起作用。我沒有登錄。 – user883883

+0

@ user883883 - 您需要在POST命令的網頁上輸入正確的登錄字段名稱。我例子中的名字是任意的。此外,根據您的實施情況,您可能不需要登錄。這完全取決於您想要執行的操作。 –