Android中的HttpURLConnection网络请求方式
2016-09-21 14:24
531 查看
转自:http://blog.csdn.net/ti2016/article/details/51873289
get请求
post请求
<span style="white-space:pre"> </span>String path="http://www.baidu.com"; String param="hehehe"; //新建一个URL对象 try { URL url = new URL(path); // 打开一个HttpURLConnection连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置请求方式请求post conn.setRequestMethod("POST"); // Post请求必须设置允许输出 conn.setDoOutput(true); // Post请求不能使用缓存 conn.setUseCaches(false); // 配置请求Content-Type Content-Length conn.addRequestProperty("Content-Length", param.length()+""); conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStream out = conn.getOutputStream(); out.write(param.getBytes()); // 开始连接 conn.connect(); int code = conn.getResponseCode(); // 判断请求是否成功 if(code==200){ InputStream in= conn.getInputStream(); //把字节流转化成字符流 InputStreamReader InputStreamReader isr=new InputStreamReader(in); //把字符流转换成缓冲字符流 BufferedReader br=new BufferedReader(isr); // 创建一个StringBuffer StringBuffer sb=new StringBuffer(); String str=""; while((str=br.readLine())!=null){ sb.append(str); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
get请求
<span style="white-space:pre"> </span>//网址 String path="http://www.baidu.com"; try { //新建一个URL对象 URL url = new URL(path); // 打开一个HttpURLConnection连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置请求方式get请求 conn.setRequestMethod("GET"); // 设置连接超时时间 conn.setConnectTimeout(5000); // //再设置超时时间 conn.setReadTimeout(5000); // 开始连接 conn.connect(); // 判断请求是否成功 成功码为200 if(200==conn.getResponseCode()){ InputStream inputStream = conn.getInputStream(); //把字节流转化成字符流 InputStreamReader InputStreamReader isr=new InputStreamReader(inputStream); //把字符流转换成缓冲字符流 BufferedReader br=new BufferedReader(isr); // 创建一个StringBuffer StringBuffer sb=new StringBuffer(); String str=""; while((str=br.readLine())!=null){ sb.append(str); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
相关文章推荐
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- Android 中的两种请求网络资源的方式:HttpClient和HttpURLConnection
- Android中的HttpURLConnection网络请求方式
- Android中的HttpURLConnection网络请求方式
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- Android HttpURLConnection获取网络下载图片POST请求方式
- Android使用HttpURLConnection请求网络资源
- 安卓访问网络常用的3种方式(httpClient, httpUrlConnection,android-query ajax)及cookie处理
- Android基础入门教程——7.1.3 Android HTTP请求方式-HttpURLConnection
- Android中的post和get请求(HttpURLConnection)方式
- Android之Http通信——3.Android HTTP请求方式:HttpURLConnection
- Android HttpUrlConnection 发送网络请求步骤总结
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- Android中网络通信方式的简单汇总(HttpURLConnection、HttpClient、Socket等)
- Android网络常用的请求---HttpURLConnection和HttpClient
- Android的三种网络联接方式(URL / HttpURLConnection | HttpClient | InetAddress )
- android网络技术之——HttpURLConnection请求
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- Android中HttpURLConnection网络请求
- Android两种网络访问方式(HttpClient HttpUrlConnection)的比较及三种传参方式介绍