如何从android发布表单数据?

时间:2011-08-08 12:25:04

标签: java android http browser

我想使用Android java“模拟”浏览器并发布登录数据。我试过这个How to submit HTML form data (method=POST) using JAVA 但我失败了.....

的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>

我得到的标题:(使用chrome)

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

任何人都知道如何发布登录数据?????????

2 个答案:

答案 0 :(得分: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()));

答案 1 :(得分:0)

使用HttpClient。我可以做帖子并处理身份验证。文件here