我不知道我刚开始问的是什么,经过几次Bings我找到了我需要的东西.
需要通过处理HTML Post表单登录到Web站点,保持会话并在网站上工作.
连接到站点的简单功能如下.
public static void connectToSite(HttpClient client, String username, String password){
List<NameValuePair> arguments = new ArrayList<NameValuePair>();
arguments.add(new BasicNameValuePair("email", username));
arguments.add(new BasicNameValuePair("pword", password));
arguments.add(new BasicNameValuePair("action", "modifyPALS"));
arguments.add(new BasicNameValuePair("Submit", "Login"));
HttpPost post = new HttpPost();
try{
post.setURI(new URI("http://www.my-target-website.com/login.php"));
post.setEntity(new UrlEncodedFormEntity(arguments));
client.execute(post);
AppStatus s = getSiteConnectionStatus(client, site);
}
catch (URISyntaxException e){
Log.e("LANKAFRIENDS", "SiteConnection.connectToSite():URISyntaxException");
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我通过Singleton实现获得了一个HttpClient的单个实例.并在我想要的时候使用它.
表单属性在List中存储为BasicNameVluePair.创建一个HttpPost对象,通过setURI方法提供url.然后该列表作为实体提供给HttpPost对象并最终执行.
有些事情非常明显,如果你需要找到的话,StackOverflow和Bing中有很多指令.
我猜那就是:)
| 归档时间: |
|
| 查看次数: |
5791 次 |
| 最近记录: |