gpt4 book ai didi

java - 如何使用Java登录网站

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:00:37 27 4
gpt4 key购买 nike

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

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 行,它会给出另一个错误。

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 进行基于表单的身份验证吗?

谢谢。

最佳答案

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

其次,试试这个:

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

如果您碰巧在“另一边”并希望防止您的用户受苦,请在重定向 POST 时使用响应代码 303(参见其他) > 请求 GET,而不是常见的 302301(根据 RFC)。常规浏览器往往很好,违反规则并且不要求我们确认这些重定向,但许多移动浏览器仍然这样做。

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

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

我在 myoffice.bt.com 上尝试了登录表单,JavaScript 中发生了一些事情。

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

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

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

尝试将其中的部分或全部(至少 USERPASSWORD)添加到您的 PostMethod,并确保您提交给正确的网址。

关于java - 如何使用Java登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3091053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com