尝试模拟表单的{Angular 2 http帖子

时间:2016-06-15 13:44:51

标签: angular ionic2 angular2-http

我正在制作一个需要对现有php网站进行身份验证的ionic2应用程序,然后在其中执行一些请求。我没有任何控制权或访问该网站的来源。 由于我正在使用ionic2,我不应该有CORS问题,我还在chrome中手动禁用它以进行测试

我创建了一个服务,它在data / login.php上对页面进行登录请求 问题是登录总是失败,_body总是“”。

此外,我不知道如何获取和存储设置为保持会话的cookie。

以下是登录服务的相关代码:

doLogin(username, password) {

    var headers = new Headers();
    headers.append('content-type', 'application/x-www-form-urlencoded')

    var params = new URLSearchParams();
    params.append('usr', username);
    params.append('psw', password);
    params.append('invia_annuario', 'Accedi');


    this.http.post(this.loginPage, params.toString(), { headers: headers })
        .subscribe(
        data => {

            console.log(data);


        },
        Error => { console.log(Error) },
        () => { console.log("Done") }
        );
}

以下是来自浏览器的真实请求

正确的要求:

POST /data/login.php HTTP/1.1
Host: [website]
Connection: keep-alive
Content-Length: 55
Cache-Control: max-age=0
Origin: [website]
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.33 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://www.lions.it/data/login.php
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

请求数据: usr=[username]&psw=[password]&invia_annuario=Accedi

响应:

HTTP/1.1 200 OK
Date: Wed, 15 Jun 2016 13:34:24 GMT
Server: Apache
X-Powered-By: PHP/5.5.33
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Refresh: 0; URL=./index.php
Set-Cookie: PHPSESSID=6qp5p3f8tm7bm3hqgttfqk20m1; path=/
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

我的制造要求:

POST /data/login.php HTTP/1.1
Host: [website]
Connection: keep-alive
Content-Length: 55
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.33 Safari/537.36
content-type: application/x-www-form-urlencoded
Accept: */*
Referer: http://localhost:8100/?ionicplatform=android
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: PHPSESSID=ua37v7sgojddlnt2d961t5gu33; legatus_post_views_count_22=1

响应:

HTTP/1.1 200 OK
Date: Wed, 15 Jun 2016 13:28:11 GMT
Server: Apache
X-Powered-By: PHP/5.5.33
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Refresh: 0; URL=./index.php
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

如何正确地向远程站点发送请求以及如何获取该会话cookie?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用set方法来构建表单数据:

var params = new URLSearchParams();
params.set('usr', username);
params.set('psw', password);
params.set('invia_annuario', 'Accedi');

请参阅此plunkr:https://plnkr.co/edit/DknaczJrInjDnmOkLR6r?p=preview