如何登录到一个网站? [英] How to login to a website?

查看:224
本文介绍了如何登录到一个网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个Java程序登录到 ORKUT 。我使用下面的程序去做。我从一些网站上复制它。现在我想用它 ORKUT 。但我有一些线路的一些问题。

Q1。凡提供登录页面的URL(我认为在新的HTTPGET(.....))?我说的对不?

Q2。什么参数传递给HTTPPost的构造函数()。如果我们有通过在登录网页的HTML源代码的形式元素的action属性的值,那么请确认。

Q3。在orkut登录页的形式元素属性。

 的onsubmit =返回(gaia_onLoginSubmit());

我需要在以下code的任何变化,由于上述属性的presence?

Q4。我怎样才能得到网页的HTML源代码登录后?

 进口的java.util.ArrayList;
进口的java.util.List;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.cookie.Cookie;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.apache.http.protocol.HTTP;
公共类ClientFormLogin {公共静态无效的主要(字串[] args)抛出异常{    DefaultHttpClient的HttpClient =新DefaultHttpClient();    HTTPGET HTTPGET =新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\");    HTT presponse响应= httpclient.execute(HTTPGET);
    HttpEntity实体= response.getEntity();    的System.out.println(登录形式GET:+ response.getStatusLine());
    如果(实体!= NULL){
        entity.consumeContent();
    }
    的System.out.println(初始设置的Cookie:);
    清单< Cookie和GT;饼干= httpclient.getCookieStore()的getCookies()。
    如果(cookies.isEmpty()){
        的System.out.println(无);
    }其他{
        的for(int i = 0; I< cookies.size();我++){
            的System.out.println( - + cookies.get(ⅰ)的ToString());
        }
    }    HttpPost httpost =新HttpPost(https://www.google.com/accounts/ServiceLoginAuth?service=orkut);    清单<&的NameValuePair GT; nvps =新的ArrayList<&的NameValuePair GT;();
    nvps.add(新BasicNameValuePair(电子邮件,用户名));
    nvps.add(新BasicNameValuePair(的passwd,密码));    httpost.setEntity(新UrlEn codedFormEntity(nvps,HTTP.UTF_8));    响应= httpclient.execute(httpost);
    实体= response.getEntity();    的System.out.println(登录形式GET:+ response.getStatusLine());
    如果(实体!= NULL){
        entity.consumeContent();
    }    的System.out.println(后登录的Cookie:);
    饼干= httpclient.getCookieStore()的getCookies()。
    如果(cookies.isEmpty()){
        的System.out.println(无);
    }其他{
        的for(int i = 0; I< cookies.size();我++){
            的System.out.println( - + cookies.get(ⅰ)的ToString());
        }
    }    //当HttpClient的实例不再需要,
    //关闭连接管理器,以确保
    //所有系统资源的直接释放
    。httpclient.getConnectionManager()关机();


解决方案

Q1:

要做到这一点的标准方法是一个HTTP POST与登录信息作为方法车身参数登录URL。这通常是用户名和密码(或潜在的密码的散列)。

的会话cookie可以从响应头(或他们的cookie)进行检索,然后或者添加为属性未来HTTP GET的网站,或请求头。

Q2:

我认为这取决于在网站上。不知道 - 尝试与Firefox和Live HTTP头扩展修修补补。

Q3:

大概没有。

Q4:

使用Method.getResponseBodyAsString或Method.getResponseBody或Method.getResponseBodyAsStream一个HTTP GET后检索响应,这将包含网页的HTML源代码。

I want to login to the ORKUT through a java program. I am using the following program to do it. I have copied it from some website. Now I want to use it for ORKUT. But I have some questions regarding some lines.

Q1. Where to give the URL of the login page (I think in the new HTTPGET("....."))? Am I right or not?

Q2. What argument to pass to the constructor of HTTPPost(""). If we have to pass the value of the "action" attribute of the "form" element in the html source of the login webpage, then please confirm it.

Q3. The "form" element of the ORKUT login page has the attribute

onsubmit="return(gaia_onLoginSubmit());"

Do I need any changes in the following code due to the presence of above attribute ?

Q4. How can I get html source of webpages after login?

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();

解决方案

Q1:

The standard way to do this is a HTTP POST to the login URL with login information as parameters in the method body. This usually is username and password (or potentially, a hash of the password).

The session cookies can be retrieved from the response headers (or their cookies), and then either added as attributes to a future HTTP GET for the site, or as request headers.

Q2:

I think it depends on the site. Not sure -- try tinkering with Firefox and the Live HTTP Headers extension.

Q3:

Probably not.

Q4:

use the Method.getResponseBodyAsString OR Method.getResponseBody OR Method.getResponseBodyAsStream after an HTTP GET to retrieve the response, which will contain the HTML source for the page.

这篇关于如何登录到一个网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆