HttpURLConnection与HttpClient的Get与Post请求数据流程

来源:互联网 发布:微博怎么绑定域名 编辑:程序博客网 时间:2024/05/21 07:12

HttpURLConnection与HttpClient的Get与Post请求数据流程
在开发一年的时间里,一直用着已经封装好的Volley框架,时间久了却忘记了基本的网络请求方式,问了把基础巩固,随便写写,加深自己对网络请求的认识:
网络请求有两种方式,HttpClient与HttpURLConnection,他们都可以做get和post请求。
下面一一来分析:
HttpClient的Get请求流程,详情见注释:

private void doHttpGet() {        try {            // 1,先对数据进行键值对的封装,键值对BasicNameValuePair            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();            list.add(new BasicNameValuePair("s", "ss"));            list.add(new BasicNameValuePair("info", "from & jerry"));            // 2,对数据进行编码,url的编码            String data = URLEncodedUtils.format(list, "utf-8");            //3.new 一个HttpClient对象            HttpClient client = new DefaultHttpClient();            //4.设置HttpClient对象的参数,即请求参数HttpConnectionParams            HttpConnectionParams.setConnectionTimeout(client.getParams(), 3000);            HttpConnectionParams.setSoTimeout(client.getParams(), 3000);            //5.new一个HttpGet请求,并把数据跟在URL后面            HttpGet get = new HttpGet("http://www.baidu.com/todo?" + data);            //6.new 一个HttpResponse,相应对象,执行请求的返回相应对象            HttpResponse response = client.execute(get);            //7.得到相应数据,response.getEntity()            if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {                String str = EntityUtils.toString(response.getEntity());            }        } catch (Exception e) {            e.printStackTrace();        }    }

HttpClient的Post请求流程,与get请求大同小异,区别是post数据是放在实体对象中,详情见注释:

private void doHttpPost() {        // 数据        List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();        list.add(new BasicNameValuePair("s", "ss"));        list.add(new BasicNameValuePair("info", "from & jerry"));        HttpClient client = new DefaultHttpClient();        HttpConnectionParams.setConnectionTimeout(client.getParams(), 3000);        HttpConnectionParams.setSoTimeout(client.getParams(), 3000);        HttpPost post = new HttpPost("http://www.baidu.com");        try {            //设置键值对数据于UrlEncodedFormEntity 对象            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);            //设置提交的表单实体对象            post.setEntity(entity);            HttpResponse response = client.execute(post);            String result = EntityUtils.toString(response.getEntity(), "utf-8");        } catch (UnsupportedEncodingException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (ClientProtocolException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }           }

HttpURLConnection的Get请求流程,详情见注释:

private void doUrlGet() {        try {            // 1,先对数据进行键值对的封装,键值对BasicNameValuePair            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();            list.add(new BasicNameValuePair("s", "ss"));            list.add(new BasicNameValuePair("info", "from & jerry"));            // 2,对数据进行编码,url的编码            String data = URLEncodedUtils.format(list, "utf-8");            //3.通过URL对象的openConnection()得到一个HttpURLConnection 对象            URL url = new URL("http://www.baidu.com/todo?" + data);            HttpURLConnection connection = (HttpURLConnection) url                    .openConnection();            //4.设置HttpURLConnection对象的参数,即请求参数            connection.setRequestMethod("GET");// 必须大写            connection.setDoInput(true);            connection.setUseCaches(false);            connection.setConnectTimeout(3000);            connection.setReadTimeout(3000);            //5.得到conn的输入流,即相应返回的数据            InputStream in = connection.getInputStream();            //6.读取数据            //**********此处省略            //7.关闭连接            in.close();            connection.disconnect();// 关闭连接        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }

HttpURLConnection的Post请求流程,与get请求大同小异,区别是post数据是放在conn的输出流中,详情见注释:

private void doUrlPost() {        try {            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();            list.add(new BasicNameValuePair("s", "ss"));            list.add(new BasicNameValuePair("info", "from & jerry"));            URL url = new URL("http://www.baidu.com/todo");            HttpURLConnection connection = (HttpURLConnection) url                    .openConnection();            connection.setRequestMethod("POST");// post请求必须写,默认是get请求            connection.setDoInput(true);// 发送数据            connection.setDoOutput(true);// 接收数据            connection.setDefaultUseCaches(false);            connection.setRequestProperty("content-type", "application/x-form");            connection.connect();            String data = URLEncodedUtils.format(list, "utf-8");            OutputStream out = connection.getOutputStream();// 先获得输出流,写数据,在获得输入流            // 先发送,在接收            out.write(data.getBytes());            out.flush();            out.close();            InputStream in = connection.getInputStream();            // 读取数据            in.close();        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

ok分析完成….

0 0
原创粉丝点击