【问题标题】:Connect to website and keep session?连接到网站并保持会话?
【发布时间】:2011-03-31 09:53:49
【问题描述】:

我正在尝试连接到需要登录的网站(下面的源代码),然后浏览它以下载一些文件。我已经设法使用此代码为另一个网站执行此操作:

public void initConnection(String _path, Map<String,String> _parameters) throws IOException {

    String data = convertMapToParams(_parameters);

    // Send data
    URL url = new URL(host + "/" + _path);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    wr.close();

    sessionCookie = conn.getHeaderField("Set-Cookie");
    sessionCookie = sessionCookie.substring(0,sessionCookie.indexOf(";"));
}
public List<String> getHtml(String _path, Map<String, String> _parameters) throws IOException {

    String data = convertMapToParams(_parameters);

    URL url = new URL(host + "/" + _path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);

    conn.setRequestProperty("Cookie", sessionCookie);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    wr.close();

    List<String> list = new LinkedList<String>();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        list.add(line);
    }

    rd.close();

    return list;
}

问题是在这个网站上,当我这样做时:

sessionCookie = conn.getHeaderField("Set-Cookie");

我得到 sessionCookie == "null",所以我无法获取任何 cookie 来保持会话打开。 如果我从 conn 变量中获取标头以检查其中是否有任何 cookie 字段,我会得到这个(来自 IntelliJ IDEA 调试器):

[0] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2085}"null=[HTTP/1.1 200 OK]"
[1] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2093}"X-AspNet-Version=[2.0.50727]"
[2] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2102}"Date=[Wed, 18 Aug 2010 07:32:37 GMT]"
[3] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2111}"Content-Length=[3686]"
[4] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2120}"Content-Type=[text/html; charset=utf-8]"
[5] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2129}"Server=[Microsoft-IIS/6.0]"
[6] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2138}"X-Powered-By=[ASP.NET]"
[7] = {java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry@2147}"Cache-Control=[private]"

但是使用火狐插件“HttpFox”检查是否有cookies,我发现有:

(Request-Line)  POST /companias/entrada.aspx HTTP/1.1
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 6.1; es-ES; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8
Accept-Language es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Cookie  __utma=235757843.1141928071.1280949246.1282083861.1282114987.11; __utmz=235757843.1280949246.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=235757843
Content-Type    application/x-www-form-urlencoded
Content-Length  381

让我感到困惑的另一件事是源代码“__VIEWSTATE”、“__EVENTVALIDATION”、“__EVENTTARGET”、“__LASTFOCUS”和“__EVENTARGUMENT”中的这些字段。因为我一直在搜索关于他们的信息,如果我理解正确的话,你可以使用 VIEWSTATE 来控制用户的会话,但我不知道它是如何工作的。

所以,简而言之,在另一个网站上,我使用了那个简单的“getheaderField(“Set-Cookie”)”来获取 cookie 并保持会话处于活动状态,但现在我不知道该网站是否使用 cookie 或如果不是,我也不知道 cookie 是否会成为可行的方法,或者我是否必须使用此 VIEWSTATE 字段来这样做。

我对 Java 还不是很有经验,对连接方面的经验也较少,我在这里被推荐使用 Apache HttpClient 来处理这些事情,我正在阅读它,但我现在有很多混合的东西,我首先需要知道如何使用这个网站。

最后,这是本网站源代码的一部分:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

    <head><title>
    Steps Peritaciones S.L.
</title><link href="../Styles/general.css" rel="stylesheet" type="text/css" />
        <style type="text/css">
        </style>
    </head>

    <body>
        <form name="form1" method="post" action="entrada.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">

<div>
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODcxMzI1MDYzZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUbTG9naW5TdGVwcyRMb2dpbkltYWdlQnV0dG9udl7bDlN22j9J5Z5UXZi+FLbU6hk=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

<div>

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBAKZ/NOFAgK6jd26DgKovcvMBwKV8YLlBGCk0AytR6jZVZxOJwJ59H/uIN21" />
</div>
            <div class="logoEntrada">
                <img src="../images/logo_steps_p.gif" alt="Steps Peritaciones S.L." />
            </div>
            <div class="LoginForm" >
                <br />
                <br />
                <span id="Label1" class="TitolEntrada">Acceso Compañias</span>

                <br />
            </div>
            <div class="LoginForm">
                <center>
                    <table class="LoginBox" cellspacing="0" cellpadding="4" border="0" id="LoginSteps" style="background-color:#E3EAEB;border-color:#E6E2D8;border-width:1px;border-style:Solid;border-collapse:collapse;">
    <tr>
        <td><table cellpadding="0" border="0" style="color:#333333;font-family:Verdana;font-size:1em;width:234px;">
            <tr>
                <td align="center" style="color:White;background-color:#1C5E55;font-size:1em;font-weight:bold;">Entrada</td>

            </tr><tr>
                <td><label for="LoginSteps_UserName">Usuario:</label></td>
            </tr><tr>
                <td><input name="LoginSteps$UserName" type="text" id="LoginSteps_UserName" style="font-size:1em;width:171px;" /><span id="LoginSteps_UserNameRequired" title="El nombre de usuario es obligatorio." style="color:Red;visibility:hidden;">*</span></td>
            </tr><tr>
                <td><label for="LoginSteps_Password">Contraseña:</label></td>
            </tr><tr>

                <td><input name="LoginSteps$Password" type="password" id="LoginSteps_Password" style="font-size:1em;width:171px;" /><span id="LoginSteps_PasswordRequired" title="La contraseña es obligatoria." style="color:Red;visibility:hidden;">*</span></td>
            </tr><tr>
                <td align="right"><input type="submit" name="LoginSteps$LoginButton" value="Entrar" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;LoginSteps$LoginButton&quot;, &quot;&quot;, true, &quot;LoginSteps&quot;, &quot;&quot;, false, false))" id="LoginSteps_LoginButton" style="color:#1C5E55;background-color:White;border-color:#C5BBAF;border-width:1px;border-style:Solid;font-family:Verdana;font-size:1em;" /></td>
            </tr>
        </table></td>
    </tr>
</table>
                </center>
            </div>



<script type="text/javascript">
//<![CDATA[
var LoginSteps_UserNameRequired = document.all ? document.all["LoginSteps_UserNameRequired"] : document.getElementById("LoginSteps_UserNameRequired");
LoginSteps_UserNameRequired.controltovalidate = "LoginSteps_UserName";
LoginSteps_UserNameRequired.errormessage = "El nombre de usuario es obligatorio.";
LoginSteps_UserNameRequired.validationGroup = "LoginSteps";
LoginSteps_UserNameRequired.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
LoginSteps_UserNameRequired.initialvalue = "";
var LoginSteps_PasswordRequired = document.all ? document.all["LoginSteps_PasswordRequired"] : document.getElementById("LoginSteps_PasswordRequired");
LoginSteps_PasswordRequired.controltovalidate = "LoginSteps_Password";
LoginSteps_PasswordRequired.errormessage = "La contraseña es obligatoria.";
LoginSteps_PasswordRequired.validationGroup = "LoginSteps";
LoginSteps_PasswordRequired.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
LoginSteps_PasswordRequired.initialvalue = "";
//]]>
</script>


<script type="text/javascript">
//<![CDATA[

var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
    ValidatorOnLoad();
}

function ValidatorOnSubmit() {
    if (Page_ValidationActive) {
        return ValidatorCommonOnSubmit();
    }
    else {
        return true;
    }
}
        WebForm_AutoFocus('LoginSteps');Sys.Application.initialize();

document.getElementById('LoginSteps_UserNameRequired').dispose = function() {
    Array.remove(Page_Validators, document.getElementById('LoginSteps_UserNameRequired'));
}

document.getElementById('LoginSteps_PasswordRequired').dispose = function() {
    Array.remove(Page_Validators, document.getElementById('LoginSteps_PasswordRequired'));
}
//]]>

谢谢,我希望一篇文章中的代码不要太多:S

P.D.:这个网站属于我的工作,我已经授权访问它们,所以这不是任何黑客行为,我只是想自动化这个过程并在我使用它的同时学习

【问题讨论】:

    标签: java html session asp.net


    【解决方案1】:

    天啊,你都是手工做的吗?我真的建议您改用HtmlUnit,因为它允许您使用具有所有功能的虚拟 Web 客户端和更高级别的 API,使您可以专注于网站交互,而不是手动打开流。

    【讨论】:

    • -1 因为这并不是真正的答案。说“问题是 x,但我建议你做 y”是一回事。说“天哪,这太难了,我就是这么做的”。
    • 特别是因为这个问题非常具体。也就是说,会话/cookie 处理。您的回答完全无法解决这个具体问题。
    • 是的,当然,除了 session/cookie 处理是一件很难手工完成的事情,HtmlClient 精确地轻松处理。
    • Ops 我无法将 2 个答案标记为正确,但尽管本教程将帮助我手动管理 cookie,但我认为这个 HtmlUnit 看起来很有趣。也许如果我在这之前知道这一点,我就不会走“艰难的道路”了:) 谢谢你的信息!
    【解决方案2】:

    您也可以使用HttpClient

    这是相同的教程:

    http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/

    检查以下与 cookie 相关的内容(状态管理)

    http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/statemgmt.html

    【讨论】:

    • 谢谢,实际上我几乎阅读了教程的第 1 点,完全错过了 cookie 部分!
    猜你喜欢
    • 2014-07-15
    • 2015-03-21
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多