菜鸟教程小白 发表于 2022-12-11 20:12:47

ios - Alamofire API(Digest Access Authentication) 数据访问错误


                                            <p><p>我尝试 Alamofire 与我的服务器 API 通信以获取 JSON 数据。
我的 API 使用摘要式访问身份验证,但我最初在面对服务器挑战时遇到了问题,并设法通过以下代码克服。</p>

<pre><code>    let userNameValue = &#34;username&#34;
    let passwordValue = &#34;password&#34;
    let credential = URLCredential(user: userNameValue, password: passwordValue, persistence: .forSession)      
    let sessionMananager = Alamofire.SessionManager.default
    let request = sessionMananager.request(&#34;http://httpbin.org/basic-auth/\(userNameValue)/\(passwordValue)&#34;)
      .authenticate(usingCredential: credential)
      .responseJSON { response in
            print(&#34;Response: \(String(describing: response.response))&#34;) // http url response
            print(&#34;Result: \(response.result)&#34;)                         // response serialization result
    }
</code></pre>

<p><strong>输出看起来像</strong></p>

<pre><code>    Response:   
   { Status Code: 500, Headers {
         Connection =   (
               close
         );
         &#34;Content-Length&#34; =   (
                0
         );
         &#34;Content-Type&#34; =   (
                &#34;text/html; charset=UTF-8&#34;
         );
   } }
    Result: FAILURE
</code></pre>

<p>经过一番搜索,我将 .responseJSON 更改为 .responseString,输出更改如下</p>

<pre><code>    Response:   
   { Status Code: 500, Headers {
         Connection =   (
               close
         );
         &#34;Content-Length&#34; =   (
                0
         );
         &#34;Content-Type&#34; =   (
                &#34;text/html; charset=UTF-8&#34;
         );
   } }
    Result: SUCCESS
</code></pre>

<p>为了确保挑战得到处理,我输入了错误的密码并尝试使用 .responseString,它给出的输出是状态代码:401。</p>

<p><strong>需要建议</strong> </p>

<p>要从 API 获取数据,</p>

<p>即使 Status Code: 500 是内部错误,我也不认为是服务器的问题。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我是这样做的:</p>

<pre><code>let sessionMananager = Alamofire.SessionManager.default
    let credential = URLCredential(user: &#34;bruce&#34;, password: &#34;dickinson&#34;, persistence: .forSession)

    sessionMananager.request(&#34;https://httpbin.org/digest-auth/undefined/bruce/dickinson&#34;)
      .authenticate(usingCredential: credential)
      .responseJSON { response in
            print(&#34;Result: \(String(describing: response.response?.statusCode))&#34;)
            print(response.result)
            print(response.description)
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Alamofire API(Digest Access Authentication) 数据访问错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50634697/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50634697/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Alamofire API(Digest Access Authentication) 数据访问错误