在Java中发送HTTP标头的问题

时间:2010-11-23 00:34:40

标签: java android json http httpclient

我是否正确发送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。

2 个答案:

答案 0 :(得分:4)

您未在此代码中设置任何标头。使用setHeader()设置标头。 HttpClient的documentation也涵盖了这一点,previous question提供的信息也是如此。

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

答案 1 :(得分:0)

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

  

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

它对我有用。