2

我想通过 java 程序登录到ORKUT 。我正在使用以下程序来做到这一点。我是从某个网站复制的。现在我想将它用于ORKUT。但是我对某些行有一些疑问。

Q1。在哪里提供登录页面的 URL(我认为在新的 HTTPGET(".....") 中)?我是对还是错?

Q2。将什么参数传递给 HTTPPost("") 的构造函数。如果我们必须在登录网页的html源中传递“form”元素的“action”属性的值,那么请确认。

Q3。ORKUT 登录页面的“form”元素具有属性

onsubmit="return(gaia_onLoginSubmit());"

由于上述属性的存在,我是否需要对以下代码进行任何更改?

Q4。登录后如何获取网页的html源?

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


public class ClientFormLogin {

public static void main(String[] args) throws Exception {

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("https://www.google.com/accounts/ServiceLogin?service=orkut&hl=en-US&rm=false&continue=http%3A%2F%2Fwww.orkut.com%2FRedirLogin%3Fmsg%3D0%26page%3Dhttp%253A%252F%252Fwww.orkut.co.in%252FHome.aspx&cd=IN&passive=true&skipvpage=true&sendvemail=false");

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }
    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    HttpPost httpost = new HttpPost("https://www.google.com/accounts/ServiceLoginAuth?service=orkut");

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("Email", "username"));
    nvps.add(new BasicNameValuePair("Passwd", "password"));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = httpclient.execute(httpost);
    entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();        
4

1 回答 1

5

Q1:

执行此操作的标准方法是向登录 URL 发送 HTTP POST,其中登录信息作为方法主体中的参数。这通常是用户名和密码(或者可能是密码的哈希)。

会话 cookie 可以从响应标头(或它们的 cookie)中检索,然后作为属性添加到站点的未来 HTTP GET 中,或者作为请求标头添加。

Q2:

我认为这取决于网站。不确定——尝试修改 Firefox 和 Live HTTP Headers 扩展。

问题 3:

可能不是。

问题 4:

在 HTTP GET 之后使用 Method.getResponseBodyAsString OR Method.getResponseBody OR Method.getResponseBodyAsStream 来检索响应,该响应将包含页面的 HTML 源。

于 2009-09-15T02:15:40.180 回答