![컬 인증은 작동하지만 다른 페이지에 액세스할 수 없습니다](https://linux55.com/image/60985/%EC%BB%AC%20%EC%9D%B8%EC%A6%9D%EC%9D%80%20%EC%9E%91%EB%8F%99%ED%95%98%EC%A7%80%EB%A7%8C%20%EB%8B%A4%EB%A5%B8%20%ED%8E%98%EC%9D%B4%EC%A7%80%EC%97%90%20%EC%95%A1%EC%84%B8%EC%8A%A4%ED%95%A0%20%EC%88%98%20%EC%97%86%EC%8A%B5%EB%8B%88%EB%8B%A4.png)
저는 cURL을 사용하여 일반적으로 웹사이트에서 수행하는 일부 프로세스를 자동화하려고 합니다.
컬과 다음 명령을 사용하여 웹사이트에 로그인할 수 있습니다.
curl -k -v -i --user "[user]:[password]" -D cookiejar.txt https://link/to/home/page
그러나 생성된 것을 사용하려고 하면쿠키병.txt해당 파일은 후속 통화에 사용되며 권한이 없습니다.
브라우저는 다음 데이터를 서버로 보냅니다.
GET /[my other page] HTTP/1.1
Host [my host]
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-US,en;q=0.5
Accept-Encoding gzip, deflate
Cookie JSESSIONID=[my session id]
Authorization Basic [my encrypted string]
Connection keep-alive
그래서 모든 매개변수도 전송되도록 두 번째 cURL 호출을 다음과 같이 변경했습니다.
curl -i -X GET -k -v \
-b cookiejar.txt \
-H "Authorization: Basic [my encrypted string]" \
-H "Host: [my host]" \
-H "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept-Language: en-US,en;q=0.5" \
-H "Connection: Keep-Alive" \
https://[my other page]
불행히도 이것은 작동하지 않습니다. 내가 생략하면승인하다제목, 401 오류가 발생합니다. 이것을 cURL 요청에 포함하면 로그인 페이지(200 OK 응답)가 표시됩니다.
적어도 문제에 대한 힌트를 제공하기 위해 콘솔에 오류가 없습니다.
이 문제를 해결하는 데 도움이 되는 아이디어를 주시면 감사하겠습니다.
답변1
이는 인증 중 리디렉션으로 인해 발생할 수 있습니다. -L
의 및 옵션을 참조하십시오 . 또한 리디렉션될 실제 페이지를 확인하기 위해 테스트해 보십시오(이 경우).--location-trusted
man curl
-w redirect_url
-L, --location
(HTTP/HTTPS) If the server reports that the requested page has moved to a different location
(indicated with a Location: header and a 3XX response code), this option will make curl redo the
request on the new place. If used together with -i, --include or -I, --head, headers from all
requested pages will be shown. When authentication is used, curl only sends its credentials to
the initial host. If a redirect takes curl to a different host, it won't be able to intercept the
user+password. See also --location-trusted on how to change this. You can limit the amount of
redirects to follow by using the --max-redirs option.
When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it
will do the following request with a GET if the HTTP response was 301, 302, or 303. If the
response code was any other 3xx code, curl will re-send the following request using the same
unmodified method.
--location-trusted
(HTTP/HTTPS) Like -L, --location, but will allow sending the name + password to all hosts that
the site may redirect to. This may or may not introduce a security breach if the site redirects
you to a site to which you'll send your authentication info (which is plaintext in the case of
HTTP Basic authentication).
-w, --write-out <format>
Defines what to display on stdout after a completed and successful operation. The format is a
string that may contain plain text mixed with any number of variables. The string can be speci‐
fied as "string", to get read from a particular file you specify it "@filename" and to tell curl
to read the format from stdin you write "@-".
The variables present in the output format will be substituted by the value or text that curl
thinks fit, as described below. All variables are specified as %{variable_name} and to output a
normal % you just write them as %%. You can output a newline by using \n, a carriage return with
\r and a tab space with \t.
NOTE: The %-symbol is a special symbol in the win32-environment, where all occurrences of % must
be doubled when using this option.
The variables available are:
redirect_url When an HTTP request was made without -L to follow redirects, this variable will
show the actual URL a redirect would take you to. (Added in 7.18.2)
답변2
드디어 원하는 페이지로 이동할 수 있었습니다.
URL 호출의 올바른 순서를 따르지 않는 것 같습니다. 이렇게 하면 필요한 페이지가 올바르게 검색됩니다.
빠른 답변에 진심으로 감사드립니다!