【问题标题】:PHP : send http post request but "Authentication failed"PHP:发送http post请求但“身份验证失败”
【发布时间】:2017-10-13 05:19:24
【问题描述】:

我正在尝试登录网站,但收到“身份验证失败”错误,好像我使用的用户名或密码有误。传递的参数是正确的,包括用户名和密码,因为我在 Java 中开发了相同的代码并且它可以工作。我发送字段时是否出错?

$cookies = array();
foreach ($http_response_header as $hdr) {
    if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
        parse_str($matches[1], $tmp);
        $cookies += $tmp;
    }
}
$cookie= reset($cookies);

$request = array(
    'http' => array(
        'method' => 'POST',
        'timeout' => 0,
        'header'=> "Accept-language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3\r\n" .
            "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" .
            "User-Agent:    Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6\r\n" .
            "Cookie: ASP.NET_SessionId=".$cookie."\r\n",
        'content' => http_build_query(array(
            '__LASTFOCUS' => '',
            '__EVENTTARGET' => '',
            '__EVENTARGUMENT' => '',
            '__VIEWSTATE' => $viewstate,
            '__VIEWSTATEGENERATOR' => $viewstategenerator,
            'ctl00$hwsid' => $hwsid,
            'ctl00$PageSessionId' => $pagesessionid,
            'ctl00$DefaultUrl' => $defaulturl,
            'ctl00$GenericErrorUrl' => $genericerrorurl,
            'ctl00$PopupElement' => '',
            'ctl00$PollingTimeoutSecs' => $pollingtimeoutsecs,
            'ctl00$bodyContent$txtUser' => $user,
            'ctl00$bodyContent$txtPassword' => $password,
            '__CALLBACKID' => '__Page',
            '__CALLBACKPARAM' => '"hwsid="'.$hwsid.'"&PageSessionId="'.$pagesessionid.'"&DefaultUrl="'.$defaulturl.'"&GenericErrorUrl="'.$genericerrorurl.'"&PopupElement="'.'"&PollingTimeoutSecs="'.$pollingtimeoutsecs.'"&txtUser="'.$user.'"&txtPassword="'.$password,
            '__EVENTVALIDATION' => $eventvalidation,
            'ctl00$bodyContent$btnLogin' => 'Conferma'

        )),
    )
);

$context = stream_context_create($request);
$res= file_get_contents($url, false, $context);
echo htmlentities($res);

用于java的工作代码如下:

cookies = initialResponse.cookies();

                initialResponse = Jsoup.connect(url+"Default.aspx")
                    .data("__LASTFOCUS", "")
                    .data("__EVENTTARGET", "")
                    .data("__EVENTARGUMENT", "")
                    .data("__VIEWSTATE", executionVal)
                    .data("__VIEWSTATEGENERATOR", vv1)
                    .data("ctl00$hwsid", a11)
                    .data("ctl00$PageSessionId", a22)
                    .data("ctl00$DefaultUrl", a33)
                    .data("ctl00$GenericErrorUrl", a44)
                    .data("ctl00$PopupElement", "")
                    .data("ctl00$PollingTimeoutSecs", a66)
                    .data("ctl00$bodyContent$txtUser", user)
                    .data("ctl00$bodyContent$txtPassword", pass)
                    .data("__CALLBACKID", "__Page")
                    .data("__CALLBACKPARAM", "hwsid="+a11+"&PageSessionId="+a22+"&DefaultUrl="+a33+"&GenericErrorUrl="+a44+"&PopupElement="+"&PollingTimeoutSecs="+a66+"&txtUser="+user+"&txtPassword="+pass)
                    .data("__EVENTVALIDATION", ltVal)
                    .data("ctl00$bodyContent$btnLogin", "Conferma") 
                    .cookies(cookies)
                    .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36")
                    .method(Method.POST)
                    .timeout(0)
                    .execute();
            }catch(UnknownHostException e){
                 JOptionPane.showMessageDialog(null, "No", "Turni", JOptionPane.ERROR_MESSAGE); 
                 System.exit(0);
            } catch (IOException e) {
                e.printStackTrace();
            }

            cookies.putAll(initialResponse.cookies());

            Document doc = null;
            try {
                doc = Jsoup.connect(u)
                  .cookies(cookies)
                  .get();

            } catch (IOException e) {
                e.printStackTrace();
            }

【问题讨论】:

  • 查看最终 URL、cookie 和 POST 正文可能会有所帮助;然后将它们与工作版本进行比较。
  • 我想我已经理解了,实际上,当我访问该网站时,会生成第一个 cookie (ASP_NET_SessionId=wpxfk3msbyghw5nlo2wlxer),一旦您使用该 cookie 发送请求帖子,该站点就会发出另一个to access and this I think you have to add (SSOAuth=DF1D63B311125E1C34689C7D16B4BE4D798E718C1A037B189A2284797B7FFB) so to the post final request, cookie would add the two cookies "ASP_NET_SessionId=wpxfk3msbyghw5nlo2wlxer; SSOAuth=DF1D63B311125E1C34689C7D16B4BE4D798E718C1A037B189A2284797B7FFB", but how do I get in the code and add the second饼干?

标签: php http-post


【解决方案1】:

我怀疑你在滥用reset()。它返回第一个数组元素的值。因此,如果您收到不止一个 cookie,您可能会遇到问题。如果您正在寻找特定的 cookie,您可以执行以下操作:

// here's what we're looking for
$target = "ASP.NET_SessionId";

// filter the array
$cookies = array_filter(
    $http_response_header,
    function($v) use ($target) {return strpos($v, "Set-Cookie: $target=") === 0;}
);

if (!empty($cookies)) {
    // here we know we only have a single entry in the array
    $cookie = reset($cookies);
    $cookie = preg_replace("/.*=([^;]*)/", "$1", $cookie);
} else {
    // no cookies received!
    $cookie = "";
}

这确实比它需要的复杂得多。最简单的做法是获取所有 cookie 并在第二个请求中将它们发回:

$cookies = array_filter(
    $http_response_header,
    function($v) {return strpos($v, "Set-Cookie:") === 0;}
);
$headers = [
    "Accept-language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3",
    "Content-Type: application/x-www-form-urlencoded; charset=utf-8",
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6",
];
foreach ($cookies as $cookie) {
    $headers[] = preg_replace("/^Set-/", "", $cookie);
}

$request = [
    "http" => [
        "method" => "POST",
        "timeout" => 0,
        "header"=> $headers,
        "content" => "..."
];

【讨论】:

  • 使用 curl 函数更简单,它会自动为您处理 cookie。
  • 正如我之前已经写过的,当我访问该网站时,会生成第一个 cookie (ASP_NET_SessionId=wpxfk3msbyghw5nlo2wlxer),一旦您发送了带有该 cookie 的请求帖子,该站点就会发出另一个能够access and this I think you have to add (SSOAuth=DF1D63B311125E1C34689C7D16B4BE4D798E718C1A037B189A2‌​284797B7FFB) so to the post final request, cookie would add the two cookies "ASP_NET_SessionId=wpxfk3msbyghw5nlo2wlxer; SSOAuth=DF1D63B311125E1C34689C7D16B4BE4D798E718C1A037B189A22‌​84797B7FFB", but how do I get in the code and添加第二个cookie?
  • 上面的第二个代码块从响应中获取所有 cookie 并将它们放入您的请求中。
  • 是正确的,但在这种情况下会将它们插入另一个 GET 请求中。但是当第二次检索所有 cookie 时,还有第二个名为“SSOAuth”的 cookie,它不存在于 POST 请求中。
  • 我不明白你的问题。上面的代码采用一组响应标头并将所有 cookie 提取出来。将它们放入您想要的任何请求中。
猜你喜欢
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多