Java发送Http请求(Get和Post)
来源:互联网 发布:知乎页面显示不正常 编辑:程序博客网 时间:2024/04/20 01:06
public class NetRequestUtil {/** * 发送http的get和post请求 ,当请求为get时,param格式为paramName1=value1¶mName2=value2;当请求为post时,param格式为json * @param url * @param param * @param method * @return */public static String sendHttpRequest(String url,String method,String param){PrintWriter out = null; BufferedReader in = null; String result = ""; URLConnection conn=null;try {if("post".equals(method.toLowerCase())){URL post_url=new URL(url);conn=post_url.openConnection();conn.setRequestProperty("content-Type","application/json");conn.setRequestProperty("chartset", "utf-8");conn.setDoOutput(true);conn.setDoInput(true);out=new PrintWriter(conn.getOutputStream());out.print(param);out.flush();}else{URL post_url=new URL(url+"?"+param);conn=post_url.openConnection();conn.setRequestProperty("chartset", "utf-8");}in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line; while ((line = in.readLine()) != null) { result += line; }} catch (Exception e) {e.printStackTrace();} finally {try {if(out != null)out.close();if(in != null)in.close();} catch (IOException e) {e.printStackTrace();}}return result;}}
阅读全文
0 0
- java发送http get 和 post 请求
- Java发送Http请求(Get和Post)
- Java发送HTTP请求(GET和POST)
- java发送HTTP请求 (get和post方法请求)
- JAVA 发送Http请求,GET、POST请求
- java 发送HTTP (get 和 post方法请求)
- Java发送HTTP的POST和GET方式请求
- java使用post和get方式发送http请求
- java之http发送post和get请求
- java发送HTTP GET POST请求
- java发送get、post http请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java 发送http请求post get
- 文件包含漏洞原理分析
- LeetCode 494. Target Sum
- 使用SQL*Loader向oracle数据库上传数据
- Tomcat:Caused by: java.lang.OutOfMemoryError: PermGen space的解决方案
- KMP算法(Python实现)
- Java发送Http请求(Get和Post)
- Angular最新教程-第一节环境搭建和新建项目
- 如何提高深度学习(和机器学习)的性能
- scrapy爬虫防ban策略总结
- [ROS Navigation Tuning Guide]翻译
- Linux的学习笔记_Day1
- 制作安装U盘
- Linux环境安装imagemagick
- 【异常解决】jxl.write.biff.JxlWriteException: Attempt to modify a referenced format