我正在制作一个 ionic2 应用程序,该应用程序需要对现有 php 站点进行身份验证,然后在其中执行一些请求。我对网站的源没有任何控制或访问权限。由于我使用的是 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?