Android中的HttpURLConnection网络请求方式
来源:互联网 发布:英雄联盟代练接单软件 编辑:程序博客网 时间:2024/06/08 04:37
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(); }
0 0
- Android中的HttpURLConnection网络请求方式
- Android中的HttpURLConnection网络请求方式
- Android中的HttpURLConnection网络请求方式
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- Android中的网络请求之HttpURLConnection
- Android HTTP协议请求网络(三)之HttpURLConnection方式
- Android HttpURLConnection获取网络下载图片POST请求方式
- Android中的post和get请求(HttpURLConnection)方式
- Android HTTP请求方式:HttpURLConnection
- android HttpURLConnection网络请求数据
- android 请求网络数据 HttpURLConnection
- Android基于HttpURLConnection网络请求
- android网络请求HttpURLConnection详解
- HttpURLConnection GET 方式 请求网络数据
- Android中的几种网络请求方式
- Android HttpUrlConnection 发送网络请求步骤总结
- 网络请求测试之HttpUrlConnection【Android】
- Android 网络请求(一)HttpURLConnection+OkHttp
- NOIP2016 第二题 回文日期
- Centos7 搭建Java7开发环境
- JS特殊符号
- c++中的联合体 union
- UEFI 及 legacy 重装系统
- Android中的HttpURLConnection网络请求方式
- Longest Palindromic Substring
- Linux Centos安装Jenkins
- 【java】理解Java垃圾回收机制
- Mybatis查询缓存
- spark sql之java和scala版本——电影受众分析系统--得分最高的10部电影;看过电影最多的前10个人
- POJ 3708 Recurrent Function 已翻译
- Java中异或^运算符的运用
- eclipse保存代码很慢,提示the user operation is waiting