如何使用Java登录网站 [英] How to login in web site using Java

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

问题描述

我想访问一些需要用户身份验证的网站 https://myoffice.bt.com 用java。我们必须先登录才能访问页面。我有以下代码。

I want to access some pages of web site https://myoffice.bt.com which requires user authentication using java. We have to sign in first to access pages. I have wriiten following code.

package root;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;


public class Url
{
 public static void main(String[] args) throws IOException
 {
  HttpClient client = new HttpClient();

  client.getParams().setParameter(
      HttpMethodParams.USER_AGENT,
      "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
  );

  client.getState().setCredentials(
     new AuthScope("https://myoffice.bt.com", 443,  AuthScope.ANY_REALM),
     new UsernamePasswordCredentials("username", "password")  ); 

  PostMethod get = new PostMethod("https://myoffice.bt.com/youraccount/default.aspx");
  get.setDoAuthentication( true );
  System.out.println(get.getFollowRedirects());
  //get.setFollowRedirects(true);


  try {
    // execute the GET
     int status = client.executeMethod( get );

     // print the status and response
     System.out.println(status + "\n" + get.getResponseBodyAsString());

     } finally {
     // release any connection resources used by the method
      get.releaseConnection();
     } 


 }

}

但它会出现以下错误。

> Jun 22, 2010 12:14:40 PM org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded
INFO: Redirect requested but followRedirects is disabled
302

如果我取消注释get.setFollowingRedirects行,则会出现另一个错误。

If I uncomment get.setFollowingRedirects line, It gives another error.

Exception in thread "main" java.lang.IllegalArgumentException: Entity enclosing requests cannot be redirected without user intervention
 at org.apache.commons.httpclient.methods.EntityEnclosingMethod.setFollowRedirects(Unknown Source)
 at root.Url.main(Url.java:30)

任何人都可以帮助我吗?我们可以使用HttpClient进行基于表单的身份验证吗?

Can any one help me here? Can we do form based authentication using HttpClient?

谢谢。

推荐答案

首先 - 请不要将您的 PostMethod 变量命名为 get

First - please don't name your PostMethod variable get.

其次,试试这个:

PostMethod post = new PostMethod("yourUrl")
{
    @Override
    public boolean getFollowRedirects()
    {
        return true;
    }
};

如果你碰巧在另一边,并希望阻止你的用户受苦,将 POST 请求重定向到 GET <时,请使用响应代码 303(参见其他) / code>,而不是常见的 302 301 (每 RFC )。常规浏览器往往很好,违反规则,不要求我们确认这些重定向,但很多移动浏览器仍然这样做。

If you ever happen to be on the "other side" and want to prevent your users from suffering, use the response code 303 (See Other) when redirecting a POST request to a GET, instead of the common 302 and 301 (per RFC). Regular browsers tend to be nice, break the rules and NOT ask us to confirm these redirects, but a lot of mobile browsers still do.

关于基于表单的身份验证的问题 - 您只需要找出要使用的参数名称(例如,查看通常登录的网站的来源),然后使用适当的值填充它们:

Regarding your question about form based authentication - you just need to figure out the parameter names to use (by looking at the source of the website where you "normally" log in, for example), and then populate them with the appropriate values:

post.addParameter("username", username);
post.addParameter("password", password);






我玩myoffice的登录表单。 bt.com,JavaScript中有一些事情发生。


I played around with the login form at myoffice.bt.com, there's a few things going on in JavaScript.

表单提交到 https://myoffice.bt.com/siteminderagent /forms/login.fcc

提交的表单元素如下( name = value ,某些值为空):

The form elements that are submitted were as follows (name=value, some values were empty):

Segment=btb.hub
SubSegment=
searchType=0
searchPlatform=BEA
lob=btb.hub
queryText=
searchText=
ctl00$masterWebpartManager$gwpCustomLogin1$CustomLogin1$UserName=your@email.com
ctl00$masterWebpartManager$gwpCustomLogin1$CustomLogin1$PWD=yourpwd
ctl00$masterWebpartManager$gwpCustomLogin1$CustomLogin1$RememberMe=on
USER=your@email.com
PASSWORD=yourpwd
SMENC=ISO-8859-1
SMLOCALE=US-EN
userFirstLoginUrl=https://myoffice.bt.com/ManageBusinessApplications/SecretQA.aspx
PrivateLoginSuccessUrl=https://myoffice.bt.com/sm/privatecreatesession.aspx?siteArea=btb.mya
PublicLoginSuccessUrl=https://myoffice.bt.com/sm/createsession.aspx?siteArea=btb.mya
target=https://myoffice.bt.com/sm/privatecreatesession.aspx?siteArea=btb.mya&TARGET=https%3a%2f%2fmyoffice.bt.com%2fdefault.aspx (hidden)
submitStatus=
smauthreason=
smagentname=
postpreservationdata=
AnonUserName=anon@myoffice.bt.com
authMode=SITEMINDER
smUrl=https://myoffice.bt.com/siteminderagent/forms/login.fcc
notSMUrl=https://myoffice.bt.com/default.aspx
smIdentifier=1

尝试添加部分或全部(至少 USER PASSWORD )你的 PostMethod ,并确保你提交的是正确的网址。

Try adding some or all of these (at least USER and PASSWORD) to your PostMethod, and make sure you are submitting to the correct URL.

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

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