我正试图以编程方式登录我的stackoverflow.com帐户。我试着像下面这样做一个卷曲,但它要求内容长度:
$ curl -X POST --user abc@gmail.com:abc https://stackoverflow.com/users/login -v
* Trying 151.101.193.69...
* Connected to stackoverflow.com (151.101.193.69) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.stackexchange.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
* Server auth using Basic with user 'abc@gmail.com'
> POST /users/login HTTP/1.1
> Host: stackoverflow.com
> Authorization: Basic YWJjQGdtYWlsLmNvbTphYmM=
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type:application/json
>
< HTTP/1.1 411 Length Required
< Content-Type: text/html; charset=us-ascii
< Content-Length: 344
< Accept-Ranges: bytes
< Date: Sat, 10 Sep 2016 14:51:27 GMT
< Via: 1.1 varnish
< Connection: keep-alive
< X-Served-By: cache-sin6924-SIN
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1473519087.232710,VS0,VE240
< X-DNS-Prefetch-Control: off
< Set-Cookie: prov=b46dabec-5ba0-92ef-b6dd-884666bea467; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>
* Connection #0 to host stackoverflow.com left intact
我也试过没有POST
。它返回200,但没有登录。
$ curl --user abc@gmail.com:abc https://stackoverflow.com/users/login -v
* Trying 151.101.193.69...
* Connected to stackoverflow.com (151.101.193.69) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.stackexchange.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
* Server auth using Basic with user 'abc@gmail.com'
> GET /users/login HTTP/1.1
> Host: stackoverflow.com
> Authorization: Basic YWJjQGdtYWlsLmNvbTphYmM=
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< X-Frame-Options: SAMEORIGIN
< X-Request-Guid: cb0c93fa-9a05-4661-bc34-c4d97225b13e
< Content-Length: 37753
< Accept-Ranges: bytes
< Date: Sat, 10 Sep 2016 14:53:41 GMT
< Via: 1.1 varnish
< Age: 0
< Connection: keep-alive
< X-Served-By: cache-sin6924-SIN
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1473519220.936412,VS0,VE257
< X-DNS-Prefetch-Control: off
我怎样才能做到这一点?
除此之外,使用api.stackexchange.com/docs/authentication将是最好的解决方案,您的问题与上面的curl语句是,您发送一个http帖子到服务器没有正文。所以你应该添加如下内容:
-d "user=value1&passwd=value2"
其中user/passwd应该是这里登录对话框的字段名-也可能登录表单包含一些隐藏字段…所以你应该先分析一下。还请记住,您让curl将cookie写入文件,以便您可以在以后的curl调用中使用它们。