在HttpClient的机器人发送HTML命令机器人、命令、HttpClient、HTML

2023-09-07 13:10:52 作者:柳橙

我试图通过解析网页相关的数据,然后允许用户将数据发送回服务器以打开一个网络接口到一个Android之一。使用简单的HttpClient code我设法得到网页,然后解析必要的信息。我不熟悉数据发送回,所以,我希望有人能提供一些线索。下面是从登录页面相关的HTML code。

I'm attempting to turn a web interface into an Android one by parsing the web page for relevant data and then allowing the user to send back data to the server. Using simple HttpClient code I have managed to get the web page and then parse the necessary information. I'm unfamiliar with sending data back, so, I'm hoping someone can shed some light. Here's the relevant HTML code from the login page.

<table cellspacing=0 cellpadding=0><tr><td valign=top align=center>


<table cellspacing=0 cellpadding=0 border=0  width=220 align=center class=table_back><tr><td>
<table cellspacing=1 cellpadding=1 border=0 width=100%>
<tr class=table_row1><form action="i.cfm?&1028&p=login&se=4" method=post name=stepform><Td align=right nowrap>&nbsp;Empire Name&nbsp;</td><td>&nbsp;<input type=text name=nic size=16 ></td></tr>
<tr class=table_row2><Td align=right>Password&nbsp;</td><td>&nbsp;<input type=password name=password size=16 ></td></tr>

<tr class=table_row1><Td align=right valign=top>Server</td><td>&nbsp;<select name=server>


<option value="0" >Normal</option>

<option value="1" >Fast</option>

<option value="2" >Slow</option>

<option value="3" >Ultra</option>

<option value="4" selected>RT</option>


</select><font class=smallfont> <a href=javascript:ch('i.cfm?popup=help&type=server');>What is this <img src=i/help.gif></a>
</td></tr>
<tr class=table_row2><Td align=right>&nbsp;IP&nbsp;</td><td>&nbsp;69.47.105.149 <font class=smallfont>(United States of America)</font></td></tr>
<tr class=table_row1><td>&nbsp;</td><td>&nbsp;<input type=submit value="  Login  " ></td></tr>

</td></tr></table></table>

正如你可以看到有需要的3个输入,在帝国名,密码,服务器,它包括5个选项。我怎么会去了HttpClient的发送此数据返回到服务器,假设我已经收集了相关的信息,形成我的Andr​​oid GUI。任何帮助是极大的AP preciated。

As you can see there are 3 inputs needed, the "Empire Name", "Password", and the "Server" which consist of 5 options. How would I go about sending this data back to the server over httpClient, assuming that I have gathered the relevant information form my Android GUI. Any help is greatly appreciated.

推荐答案

在code可能如下所示:

The code may look like the following:

private void postDataToServer() throws UnsupportedEncodingException, IOException {

    /* 
     * Taken from "action" field in the form. This can be absolute address
     * so take this into account.
     */
    String action = "i.cfm?&1028&p=login&se=4"; 
    /* This the server you want to send info. */
    String yourServer = "http://your.server.com/";

    /* This form uses "post" method. */
    HttpPost post = new HttpPost(yourServer + action);
    /* Form parameters. */
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    /* This is what the user has entered in the corresponding fields. */
    String nic = getNic();
    String password = getPassword();
    String server = getServer();

    params.add(new BasicNameValuePair("nic", nic));
    params.add(new BasicNameValuePair("password", password));
    params.add(new BasicNameValuePair("server", server));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

    HttpClient client = new DefaultHttpClient();

    post.setEntity(entity);
    client.execute(post);
}

也许这页面也有所帮助。