我目前正在做一个小项目。该项目的目的是登录网站并获取/处理网站上的信息。此外,我想打开一些链接并搜索它们。
服务器端看起来像这样:
您需要登录到一个 php 站点。成功登录后,您将获得一个会话并将被重定向到 foo.username.bar.php(已更改)。
使用此代码:
BufferedReader in = null;
String data = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", user));
nameValuePairs.add(new BasicNameValuePair("passwort", pass));
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(website);
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data; …Run Code Online (Sandbox Code Playgroud)