java - 如何在 Android 中发送 HTTP 基本身份验证 header ?

标签 java android json httpclient http-authentication

我不确定如何发送 HTTP 身份验证 header 。

我有以下 HttpClient 来获取请求,但不确定如何发送请求?

public class RestClient extends AsyncTask<String, Void, JSONObject> {
        private String convertStreamToString(InputStream is) {
            /*
             * To convert the InputStream to String we use the
             * BufferedReader.readLine() method. We iterate until the
             * BufferedReader return null which means there's no more data to
             * read. Each line will appended to a StringBuilder and returned as
             * String.
             */
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return sb.toString();
        }

        /*
         * This is a test function which will connects to a given rest service
         * and prints it's response to Android Log with labels "Praeda".
         */
        public JSONObject connect(String url) {
            HttpClient httpclient = new DefaultHttpClient();

            // Prepare a request object
            HttpGet httpget = new HttpGet(url);

            // Execute the request
            HttpResponse response;
            try {
                response = httpclient.execute(httpget);
                // 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) {

        }
    }

最佳答案

这在 HttpClient 中有介绍 documentation在他们的 sample code .

关于java - 如何在 Android 中发送 HTTP 基本身份验证 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250968/

相关文章:

android - Jetpack Compose - TextField 在底部剪切文本

json - 如何获取 JSON 中某个键的值?

使用 gson 将 Java 日期转换为 UTC

java - Android Pie 中的图像从图库上传到服务器的问题

java - 如何使用elasticsearch Java API使用wildCardQuery方法?

android - 如何在android中动态更改整个应用程序主题

javascript - 如何从highchart中的json文件中获取数据

java - GWT 中区域设置特定的 BigDecimal 格式

java - 我们如何将 GUI 类的对象保存(序列化)到 Java 中的文件中?

android - 如何在android中组合覆盖位图和捕获的图像?