【问题标题】:Problem sending HTTP headers in Java在 Java 中发送 HTTP 标头的问题
【发布时间】:2010-11-23 00:34:40
【问题描述】:

我是否正确发送了 HTTPPost?我正在对照我的网络服务器检查我的encodedAuthString,并且可以验证该字符串是否正确。不知道为什么我无法进行身份验证。

public JSONObject connect(String url) {
            HttpClient httpclient = new DefaultHttpClient();

            // Prepare a request object
            HttpPost httppost = new HttpPost(url);

            String authString = usernameEditText.getText().toString() + ":" + passwordEditText.getText().toString();
            String encodedAuthString = Base64.encodeToString(authString.getBytes(),Base64.DEFAULT);
            String finalAuthString = "Basic " + encodedAuthString;

            // Execute the request
            HttpResponse response;
            try {  
                httppost.addHeader("Authorization", finalAuthString);

                response = httpclient.execute(httppost);
                // Examine the response status
                Log.i("Praeda", response.getStatusLine().toString());

                // Get hold of the response entity
                HttpEntity entity = response.getEntity();

                if (entity != null) {

                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    String result = convertStreamToString(instream);

                    // A Simple JSONObject Creation
                    JSONObject json = new JSONObject(result);

                    // Closing the input stream will trigger connection release
                    instream.close();

                    return json;
                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected JSONObject doInBackground(String... urls) {
            return connect(urls[0]);
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            String authorized = "200";
            String unauthorized = "401";
            String notfound = "404";
            String status = new String();

            try {
                // Get the messages array
                JSONObject response = json.getJSONObject("response");
                status = response.getString("status");

                if(status.equals(authorized))
                    Toast.makeText(getApplicationContext(), "Authorized",Toast.LENGTH_SHORT).show();
                else if (status.equals(unauthorized))
                    Toast.makeText(getApplicationContext(), "Unauthorized",Toast.LENGTH_SHORT).show();
                else if(status.equals(notfound))
                    Toast.makeText(getApplicationContext(), "Not found",Toast.LENGTH_SHORT).show();

            } catch (JSONException e) {
                System.out.println(e);
            } catch (NullPointerException e) {
                System.out.println(e);
            }
        }
    }

更新:

当我对编码字符串进行硬编码时,一切都很好。

当我使用 Base64 编码器时,我得到与编码字符串相同的结果,但服务器返回 401。

【问题讨论】:

    标签: java android json http httpclient


    【解决方案1】:

    您没有在此代码中设置任何标题。使用setHeader() 设置标题。这也包含在 HttpClient 的 documentation 中,来自您的 previous question 的信息也是如此。

    Here is a sample project 设置授权标头。请注意,它使用单独的 Base64 编码器,因为内置的编码器仅在 Android 2.2 IIRC 中出现。

    【讨论】:

    • @Sheehan Alam:考虑到我每个月都有几十个学生和更多的订阅者尝试我上面链接的示例,我对它的工作很有信心。您需要一个 identi.ca 帐户才能试用,包括点击他们发送的电子邮件确认。
    • @CommonsWare:谢谢。我已经更新了我的代码以匹配您的示例。我正在使用 addHeader() 和 setEntity() 但仍然没有运气。你的代码和我的几乎一模一样。
    • @CommonsWare:已更新。我对授权标头进行了硬编码,一切正常。当我不进行硬编码时,我会得到相同的字符串,但会从服务器返回 401。
    • @Sheehan Alam:如果您还没有这样做,请切换到我项目中包含的 Base64 编码器——我还没有尝试过 Android 2.2 中的那个。此外,如果这是您的服务器,请记录您在服务器端获取的身份验证字符串。
    • @CommonsWare:您的 Base64 编码器工作正常。我不确定为什么 Google 包含的编码器不起作用。感谢您的坚持和出色的榜样。
    【解决方案2】:

    我在字符串中看到了额外的\n,所以我没有使用默认选项,而是使用了no_wrap 选项,如下所示:

    Base64.encodeToString(authordata.getBytes(), Base64.NO_WRAP);

    它对我有用。

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      相关资源
      最近更新 更多