我没有在网上看到一个非常好的例子.如何为这样的请求添加身份验证:
(defun login-show-posts ()
(interactive)
(let ((url-request-method "GET")
(url-request-extra-headers '(("Content-Type" . "application/xml"))))
(url-retrieve "http://localhost:3000/essay/1.xml"
(lambda (status)
(switch-to-buffer (current-buffer))
))))
Run Code Online (Sandbox Code Playgroud)
例如,用户和通行证是admin:admin?
我的印象url.el主要是为交互式操作而设计,即您在未经授权的情况下进行呼叫,服务器以403"需要授权"(正确的代码?)状态响应,url.el并将向用户询问用户名和密码.
您可以在http://github.com/hdurer/fluiddb.el上查看我的代码,我尝试以编程方式执行操作.
基本上,我自己创建HTTP授权标头(base64编码正确格式化的字符串并添加正确的标头url-request-extra-headers).然后在第二步中,我需要添加建议,url-http-handle-authentication以便在传递的凭据不可接受时不会询问用户.
这感觉很像强奸,url.el但它对我有用,是我能让它发挥作用的唯一方法.
因此,您的代码看起来像这样:
(defvar xyz-user-name "admin")
(defvar xyz-password "admin")
(defvar xyz-block-authorisation nil
"Flag whether to block url.el's usual interactive authorisation procedure")
(defadvice url-http-handle-authentication (around xyz-fix)
(unless xyz-block-authorisation
ad-do-it))
(ad-activate 'url-http-handle-authentication)
(defun login-show-posts ()
(interactive)
(let ((xyz-block-authorisation t)
(url-request-method "GET")
(url-request-extra-headers
`(("Content-Type" . "application/xml")
("Authorization" . ,(concat "Basic "
(base64-encode-string
(concat xyz-user-name ":" xyz-password)))))))
(url-retrieve "http://localhost:3000/essay/1.xml"
(lambda (status)
(switch-to-buffer (current-buffer))
))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2174 次 |
| 最近记录: |