Inets http 客户端 +授权

发布于 2024-12-16 19:12:22 字数 58 浏览 5 评论 0原文

我应该如何在 httpc:request() 函数发出的 http 请求中指定客户端授权的用户/密码?

How should I specify user/password for client authorization in http request made by httpc:request() function??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅笑轻吟梦一曲 2024-12-23 19:12:22

我不认为 httpc 模块为此提供了便利。
尽管如此,实现起来并不难(如果我们谈论的是基本身份验证)。毕竟,它只是一个带有“用户:密码”对 Base64 编码的附加请求标头。
例如 Tsung 的 ts_http_common 模块 就是这样做的。

例如,以下是如何使用基本身份验证运行 HTTP PUT 请求:

auth_header(User, Pass) ->
    Encoded = base64:encode_to_string(lists:append([User,":",Pass])),
    {"Authorization","Basic " ++ Encoded}.

put_request(Url, User, Pass, Body) ->
    ContentType = "text/json",
    Headers = [auth_header(User, Pass), {"Content-Type",ContentType}],
    Options = [{body_format,binary}],
    httpc:request(put, {Url, Headers, ContentType, Body}, [], Options). 

I don't think httpc module provides facility for that.
Nevertheless it isn't hard to implement (if we are talking about Basic Authentification). After all it's just an additional request header with 'user:password' pair Base64 encoded.
For example Tsung's ts_http_common module does it.

For instance, here is how you can run HTTP PUT request with basic authentication:

auth_header(User, Pass) ->
    Encoded = base64:encode_to_string(lists:append([User,":",Pass])),
    {"Authorization","Basic " ++ Encoded}.

put_request(Url, User, Pass, Body) ->
    ContentType = "text/json",
    Headers = [auth_header(User, Pass), {"Content-Type",ContentType}],
    Options = [{body_format,binary}],
    httpc:request(put, {Url, Headers, ContentType, Body}, [], Options). 
你的呼吸 2024-12-23 19:12:22

我在文档中看到 HTTPOptions 保存了 pass 和 user:

HTTPOptions = http_options()
http_options() = [http_option()]
http_option() = {timeout, timeout()} | {connect_timeout, timeout()} | {ssl, ssloptions()} | {ossl, ssloptions()} | {essl, ssloptions()} | {autoredirect, boolean()} | {proxy_auth, {userstring(), passwordstring()}} | {version, http_version()} | {relaxed, boolean()} | {url_encode, boolean()}

文档: http:// www.erlang.org/doc/man/httpc.html#request-5

I see in doc that HTTPOptions holds pass and user:

HTTPOptions = http_options()
http_options() = [http_option()]
http_option() = {timeout, timeout()} | {connect_timeout, timeout()} | {ssl, ssloptions()} | {ossl, ssloptions()} | {essl, ssloptions()} | {autoredirect, boolean()} | {proxy_auth, {userstring(), passwordstring()}} | {version, http_version()} | {relaxed, boolean()} | {url_encode, boolean()}

documentation: http://www.erlang.org/doc/man/httpc.html#request-5

咽泪装欢 2024-12-23 19:12:22

对于摘要,您需要执行与基本操作相同的操作,但要做的更多。通常,您将在没有身份验证的情况下访问页面,获取“WWW-Authenticate”标头信息,然后使用领域和随机数来生成“Authorization”标头。 http://en.wikipedia.org/wiki/Digest_access_authentication 在底部有一个不错的例子。

一般来说,对于大多数用例来说,HTTPS + Basic 就算不是更好,也足够了。

For digest, you'll need to do the same thing as basic but more so. Generally you'll hit the page without auth, get the "WWW-Authenticate" header info, then use the realm and nonce there to generate your "Authorization" header. http://en.wikipedia.org/wiki/Digest_access_authentication has a decent example at the bottom.

Generally, HTTPS + Basic is sufficient, if not better, for most use cases.

昔梦 2024-12-23 19:12:22

尝试使用 ibrowse,它支持并且我一直在使用它! https://github.com/cmullaparthi/ibrowse

Try using ibrowse, that supports and I am have been using that! https://github.com/cmullaparthi/ibrowse

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文