【问题标题】:"401 Unauthorized" error while doing http post (cloned from curl command) in Android在 Android 中执行 http post(从 curl 命令克隆)时出现“401 Unauthorized”错误
【发布时间】:2011-03-05 05:43:17
【问题描述】:

我知道我不是第一个,但我进行了搜索但没有找到解决方案。 首先我需要这样做:

curl -user1:pass1 - X POST -d"data" http://www.mysite.com/pushes.xml

但我不想使用 curl lib 来处理如此简单的请求,因为我认为解决方案必须简单无痛。我找到了这个http login example,并尝试将其用于

    nvps.add(new BasicNameValuePair("username", "user1"));
    nvps.add(new BasicNameValuePair("password", "pass1"));

但我认为标签“用户名”和“密码”是错误的......我无法得到它们......所以我向你寻求帮助。请帮助我了解我需要做什么才能通过此登录。将保证感谢:)

附:在响应中我得到消息“HTTP Basic:访问被拒绝”。但是 curl 命令可以正常工作...

【问题讨论】:

    标签: android http post curl login


    【解决方案1】:

    检查您正在使用的 API。在我检查它之前我遇到了类似的问题,它是“用户”,而不是“用户名”。
    所以换nvps.add(new BasicNameValuePair("username", "user1"));
    nvps.add(new BasicNameValuePair("user", "user1"));试试看。

    【讨论】:

      【解决方案2】:

      如果服务器返回 401,您需要通过特殊的身份验证器或标头参数传递凭据:

          httpPost.addHeader(RestHelper.AUTHORIZATION_HEADER, RestHelper.AUTHORIZATION_HEADER_VALUE);
      
      where 
      
          AUTHORIZATION_HEADER_VALUE = "Basic " + Base64Encoder.encode ("user:password");
          AUTHORIZATION_HEADER = "Authorization";
      

          httpClient.getCredentialsProvider().setCredentials(
                  new AuthScope("server.com", 443, AuthScope.ANY_SCHEME),
                  new UsernamePasswordCredentials(LOGIN_VALUE, PASSWORD));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-25
        • 2018-01-30
        • 2023-02-23
        • 1970-01-01
        • 2011-01-22
        • 2014-04-08
        • 2013-07-26
        • 2020-07-01
        相关资源
        最近更新 更多