java利用post方法请求网络接口
2018-01-17 20:24
447 查看
使用Java对网络接口进行调用,代码如下
public class Post { public static final String POST_URL = "https://xxxxxxxxxxxxxxxxx"; public static void httpURLConnectionPOST(String parm) {//parm 你要post的数据 PrintWriter out = null; try { URL url = new URL(POST_URL); // 将url以open方法返回的urlConnection 连接强转为HttpURLConnection连接 // (标识一个url所引用的远程对象连接) // 此时cnnection只是为一个连接对象,待连接中 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置连接输出流为true,默认false (post请求是以流的方式隐式的传递参数) connection.setDoOutput(true); // 设置连接输入流为true connection.setDoInput(true); // 设置请求方式为post connection.setRequestMethod("POST"); // post请求缓存设为false connection.setUseCaches(false); // 设置该HttpURLConnection实例是否自动执行重定向 connection.setInstanceFollowRedirects(true); // 设置请求头里面的各个属性 (以下为设置内容的类型) // application/x-javascript text/xml->xml数据 // application/x-javascript->json对象 // application/x-www-form-urlencoded->表单数据 // ;charset=utf-8 必须要,不然会出现乱码 //该样例使用json对象进行请求 connection.setRequestProperty("Content-Type", "application/x-javascript;charset=utf-8"); // 建立连接 connection.connect(); // 创建输入输出流,用于往连接里面输出携带的参数,记得设置参数编码格式 OutputStreamWriter outWriter = new OutputStreamWriter(connection.getOutputStream(), "utf-8"); out = new PrintWriter(outWriter); // 将参数输出到连接 out.print(parm); // 输出完成后刷新并关闭流 out.flush(); out.close(); System.out.println(connection.getResponseCode());// 不是200的请跳楼 // 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader) BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; StringBuilder sb = new StringBuilder(); // 用来存储响应数据 // 循环读取流,若不到结尾处 while ((line = bf.readLine()) != null) { sb.append(line).append(System.getProperty("line.separator")); } bf.close(); // 日常关流 connection.disconnect(); // 销毁连接 } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- android利用httpclient实现post、get请求restful接口进行json和form表单数据提交等公共方法类
- JAVA发送http get/post请求、调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- Java利用HttpURLConnection发送post请求
- Java之发送带参数的Post请求方法
- java 发送HTTP (get 和 post方法请求)
- java实现的利用HTTP协议原理实现的GET/POST请求的web服务器
- 黑马程序员--Java基础加强--15.利用反射操作泛型IV【通过反射Method解析泛型方法思路】【通过Method对四种Type子接口类型进行解剖】【使用递归对任意复合泛型类型进行彻底解剖】【个人
- Java 以post请求方式通过json格式调用Webservice接口
- java网络请求工具方法
- java发送HTTP请求 (get和post方法请求)
- java中用httpClient请求service,httpPost方法示例
- Java发送http请求 (get 与 post方法请求)
- java实现网络交互 get、post方法