HttpURLConnection 请求
来源:互联网 发布:u盘windows无法格式化 编辑:程序博客网 时间:2024/05/29 03:12
try {//创建连接
URL url = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
// POST请求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject obj = new JSONObject();
String json = java.net.URLEncoder.encode(obj.toString(), "utf-8");
out.writeBytes(json);
out.flush();
out.close();
// 读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = URLDecoder.decode(lines, "utf-8");
sb.append(lines);
}
System.out.println(sb);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
阅读全文
0 0
- HttpURLConnection请求
- HttpURLConnection请求
- HttpURLConnection请求
- HttpURLConnection 请求
- HttpUrlConnection请求
- HttpURLConnection请求服务
- HttpURLConnection post 请求
- HttpURLConnection请求图片
- HttpURLConnection post请求
- 请求网络之HttpUrlConnection
- HttpURLConnection数据请求
- HttpUrlconnection get请求数据
- Android HttpURLConnection post请求
- httpUrlConnection请求数据
- HttpURLConnection 的get请求
- HttpURLConnection请求数据
- HttpURLConnection请求网络
- HttpURLConnection请求数据
- JDK源码获取
- 原型链模式扩展
- XSS浅学习
- 剑指offer之空格替换(Python)
- Linux常用指令
- HttpURLConnection 请求
- spring 源码学习笔记(一)—— spring ioc 之加载XML转换为BeanDefinition
- 论文管理方法
- Failed to resolve: com.android.support:26.X.X 问题的解决
- 基于Python安装TensorFlow、库安装和Jupyter Notebook
- php静态页面局部动态化
- 周六周日还不闲着,加油学习
- 【Flask】Flask的用途和目的
- 虚拟机的三种网络模式