个人对http请求的理解
16lz
2022-05-02
学了一段时间java,Android一直没有完全弄清http请求到底是怎么回事,在被折磨了很多次后 决定仔细研究一下http请求的内幕。接下来是一个菜鸟对Android中http请求的理解,大牛勿喷,全当一笑而过。
http请求首先需要一个服务器的url地址(当然这属于废话),然后你要弄清http请求的方式,Android常见的有两种get和post,关于get和post的区别相信也有很多大牛详细讲解过,这里只是简单谈一下自己的理解。get可以获得静态页面,也可以把参数放在URL字符串后面,传递给服务器。而post方法的参数是放在Http请求中。所以相比较而言的话post比get更安全,get会将请求信息暴露在URL上。然后就是你要确定使用HttpUrlconnection还是Httpclient,个人比较偏向httpclient因为所有的类都是封装好了省去了输入输出流操作。这里关于两者的区别就不作解释了,具体可找度娘。这里分Httpurlconnection和Httpclient两种情况举例get和post请求
1、Httpurlconnection
首先创建httpurlconnection对象
URL url=new URL(urlstring);HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
当然可以设定请求的基本属性
urlConn.setDoOutput(true); urlConn.setDoInput(true); urlConn.setRequestMethod("get");//设置请求方式为get
①get方式(HttpURLConnection默认使用GET方式)
InputStream iStream = null;OutputStream osOutputStream = null;if (urlConn.getResponseCode() == 200) {iStream = urlConn.getInputStream();byte[] bos = new byte[1024];osOutputStream = new ByteArrayOutputStream();int length;if ((length = iStream.read()) != -1) {osOutputStream.write(bos, 0, length);}String result=(String)osOutputStream.toString().trim();//get请求返回的结果}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
②post方式 byte[] entity = paramsString.getBytes("UTF-8");//这里的paramsString即为你要写入的参数conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//配置content-typeconn.setRequestProperty("Content-Length",String.valueOf(entity.length));//配置写入参数的长度iString = String.valueOf(entity.length);OutputStream ops = null;ops = conn.getOutputStream();ops.write(entity);ops.close();
2、Httpclient 个人觉得使用Httpclient操作更为简单,get和post方式有所不同,参照下面例子
①get方式
String httpUrl = ""; //HttpGet连接对象 HttpGet httpRequest = new HttpGet(httpUrl); //取得HttpClient对象 HttpClient httpclient = new DefaultHttpClient(); //请求HttpClient,取得HttpResponse HttpResponse httpResponse = httpclient.execute(httpRequest); //请求成功 if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String strResult = EntityUtils.toString(httpResponse.getEntity()); //取得返回的字符串 } }
②post方式
String httpUrl = ""; HttpPost httpRequest = new HttpPost(httpUrl); //HttpPost连接对象 List params = new ArrayList(); //使用NameValuePair来保存要传递的Post参数 params.add(new BasicNameValuePair("par", "HttpClient_android_Post")); //添加要传递的参数 HttpEntity httpentity = new UrlEncodedFormEntity(params, "utf-8");//设置字符集 httpRequest.setEntity(httpentity); //请求httpRequest HttpClient httpclient = new DefaultHttpClient(); //取得默认的HttpClient HttpResponse httpResponse = httpclient.execute(httpRequest);//取得HttpResponse if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String strResult = EntityUtils.toString(httpResponse.getEntity());//取得返回的字符串 } }
更多相关文章
- Android应用启动后自动创建桌面快捷方式
- Android使用http协议与服务器通信
- Android自定义字体样式Typeface的三种技术方案:Java代码的setType
- GalHttprequest类库简介——android平台上的一个轻量级的http网
- Android(安卓)网络访问框架 NiceDay 发布
- Android(安卓)Binder机制(一) Binder的设计和框架
- android工程中不自动生成Android(安卓)Dependencies的解决方式
- RelativeLayout的基本对齐方式
- 谈谈android数据存储方式