我如何发送HTTP基本认证头,在Android中? [英] How can I send HTTP Basic Authentication headers in Android?

查看:267
本文介绍了我如何发送HTTP基本认证头,在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何发送HTTP认证头。

我有以下HttpClient的GET请求,但不知道我怎么可以发送请求?

 公共类RestClient扩展的AsyncTask<字符串,无效的JSONObject> {
        私人字符串convertStreamToString(InputStream的是){
            / *
             *要InputStream中转换为字符串,我们使用
             * BufferedReader.readLine()方法。我们迭代,直到
             * BufferedReader中返回NULL,这意味着没有更多的数据
             * 读。每个行会追加到一个StringBuilder,并返回
             * 串。
             * /
            的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
            StringBuilder的SB =新的StringBuilder();

            串线= NULL;
            尝试 {
                而((行= reader.readLine())!= NULL){
                    sb.append(行+\ N);
                }
            }赶上(IOException异常E){
                e.printStackTrace();
            } 最后 {
                尝试 {
                    is.close();
                }赶上(IOException异常E){
                    e.printStackTrace();
                }
            }

            返回sb.toString();
        }

        / *
         *这是一个测试函数,它会连接到一个给定的休息服务
         *并打印的响应到Android日志使用标签Praeda。
         * /
        公众的JSONObject连接(字符串URL){
            HttpClient的HttpClient的=新DefaultHttpClient();

            // prepare请求对象
            HTTPGET HTTPGET =新HTTPGET(URL);

            //执行请求
            HTT presponse响应;
            尝试 {
                响应= httpclient.execute(HTTPGET);
                //检查响应状态
                Log.i(Praeda,response.getStatusLine()的toString());

                //弄个响应实体
                HttpEntity实体= response.getEntity();

                如果(实体!= NULL){

                    //一个简单的JSON响应读取
                    InputStream的河道= entity.getContent();
                    字符串结果= convertStreamToString(河道);

                    //一个简单的JSONObject创作
                    JSONObject的JSON =新的JSONObject的(结果);

                    //关闭输入流会触发连接释放
                    instream.close();

                    返回JSON;
                }

            }赶上(ClientProtocolException E){
                e.printStackTrace();
            }赶上(IOException异常E){
                e.printStackTrace();
            }赶上(JSONException E){
                e.printStackTrace();
            }

            返回null;
        }

        @覆盖
        受保护的JSONObject doInBackground(字符串...网址){
            返回连接(网址[0]);
        }

        @覆盖
        保护无效onPostExecute(JSONObject的JSON){

        }
    }
 

解决方案

这是覆盖在HttpClient的<一个href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html">documentation并在他们的样品code

I am not sure how to send HTTP Auth headers.

I have the following HttpClient to get requests, but not sure how I can send requests?

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) {

        }
    }

解决方案

This is covered in the HttpClient documentation and in their sample code.

这篇关于我如何发送HTTP基本认证头,在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆