java中我们有时需要调用第三方的接口,但是第三方的接口都是http形式的,这是需要用post,get,put请求类型请求

1、GET方式请求

public static String httpGet(String getUrl,Map<String, String> getHeaders) throws IOException {
    URL getURL = new URL(getUrl);
    HttpURLConnection connection = (HttpURLConnection) getURL.openConnection();

connection.setRequestProperty("accept", "*/*");
    connection.setRequestProperty("connection", "Keep-Alive");
    connection.setRequestProperty("user-agent",
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//在get请求中这是能在各个浏览器兼容json
    if(getHeaders != null) {
        for(String pKey : getHeaders.keySet()) {
            connection.setRequestProperty(pKey, getHeaders.get(pKey));
        }
    }
    connection.connect();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder sbStr = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sbStr.append(line);
    }
    bufferedReader.close();
    connection.disconnect();
    return new String(sbStr.toString().getBytes(),"utf-8");
}

2、POST方式请求

public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {
        URL postURL = new URL(postUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
        httpURLConnection.setDoOutput(true);                 
        httpURLConnection.setDoInput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setInstanceFollowRedirects(true);
        //application/json x-www-form-urlencoded
        //httpURLConnection.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Type",  "application/json;charset=utf-8");
        StringBuilder sbStr = new StringBuilder();
        if(postHeaders != null) {
            for(String pKey : postHeaders.keySet()) {
                httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
            }
        }
        if(postEntity != null) {
             JSONObject obj = new JSONObject();
             obj.put("plate",postEntity);
            PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));   
            out.println(obj);  
            out.close();  
            //httpURLConnection.getInputStream()
            BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));  //解决返回值汉字乱码的问题
              
            String inputLine;
            while ((inputLine = in.readLine()) != null) {  
                sbStr.append(inputLine);  
            }  
            in.close();  
        }
        httpURLConnection.disconnect();
        System.out.println("fsdfdf:"+sbStr.toString());
        return new String(sbStr.toString().getBytes(),"utf-8");
    }

3、PUT请求

public static String HttpPut(String postUrl,Map<String, String> postHeaders,String postEntity) throws Exception {  
        URL postURL = new URL(postUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
        httpURLConnection.setDoOutput(true);                 
        httpURLConnection.setDoInput(true);
        httpURLConnection.setRequestMethod("PUT");
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setInstanceFollowRedirects(true);
        //application/json x-www-form-urlencoded
        //httpURLConnection.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");//表单上传的模式
        httpURLConnection.setRequestProperty("Content-Type",  "application/json;charset=utf-8");//json格式上传的模式
        StringBuilder sbStr = new StringBuilder();
        if(postHeaders != null) {
            for(String pKey : postHeaders.keySet()) {
                httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
            }
        }
        if(postEntity != null) {
             JSONObject obj = new JSONObject();
             obj.put("plate","京NB1314");
            PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));   
            out.println(obj);  
            out.close();  
            BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection  
                    .getInputStream()));  
              
            String inputLine;
            while ((inputLine = in.readLine()) != null) {  
                sbStr.append(inputLine);  
            }  
            in.close();  
        }
        httpURLConnection.disconnect();
        return new String(sbStr.toString().getBytes(),"utf-8");
    }

java中怎样用post、get、put请求相关推荐

  1. JAVA中.jsp模板文件AJAX异步请求 - 数据渲染问题

    JAVA中.jsp模板文件AJAX异步请求 - 数据渲染失败,谁的过失? 后端已经查询出来结果,但是返回的API接口response查看不到对应的信息.(即:后台有,前台没有) 原因分析: 是因为AJ ...

  2. Java中SSM框架解决跨域请求

    方法一:@CrossOrigin注解的方法,只能作用在一个Controller层 直接在Controller类或方法前加上@CrossOrigin注解即可允许所有ip跨域访问 加在Controller ...

  3. java中使用okhttpsoap,Android okHttp网络请求之Retrofit+Okhttp+RxJava组合

    Retrofit介绍: Retrofit和okHttp师出同门,也是Square的开源库,它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底:比 ...

  4. java中的multipart_Java 处理 multipart/mixed 请求

    一.multipart/mixed 请求 multipart/mixed 和 multipart/form-date 都是多文件上传的格式.区别在于:multipart/form-data 是一种特殊 ...

  5. java中http解析url,java url 编码(解析http请求汉语言地址 )

    java url 编码(解析http请求中文地址 ) 在近在做项目的过程中,由于客户那边服务器上提供的有很多中文结构目录.请求要的地址不能正常运行显示出来.下面来分享一下我对http协议处理请求中文的 ...

  6. Java 中如何模拟真正的同时并发请求?

    有时需要测试一下某个功能的并发性能,又不要想借助于其他工具,索性就自己的开发语言,来一个并发请求就最方便了. java中模拟并发请求,自然是很方便的,只要多开几个线程,发起请求就好了.但是,这种请求, ...

  7. java http请求 乱码_怎么解决java中的http请求乱码

    怎么解决java中的http请求乱码 发布时间:2020-06-23 20:00:11 来源:亿速云 阅读:90 作者:元一 怎么解决java中的http请求乱码?针对这个问题,今天小编总结了这篇文章 ...

  8. java curl get_如何从Java中的curl get请求获取文件?

    我正在尝试使用API​​下载一些XBRL文件.为了做到这一点,我需要做一个卷曲的请求,就像这样:如何从Java中的curl get请求获取文件? curl -XGET http://distribut ...

  9. java请求参数_在Java中发送http的post请求,设置请求参数等等

    前几天做了一个定时导入数据的接口,需要发送http请求,第一次做这种的需求,特地记一下子, 导包 import java.text.SimpleDateFormat; import java.util ...

  10. java curl 使用方法_如何在Java中使用这个cURL POST请求?(Spotify API)

    我需要帮助在Java中发出cURL POST请求(到spotifyapi).我把我的解决办法解释得太离谱了 this article . 基于此,我尝试了以下方法: String command = ...

最新文章

  1. mysql 获取server信息_MySQL 获取服务器元数据
  2. IDEA Project Structure 配置说明
  3. 利用iframe实现ajax 跨域通信的解决方案
  4. 营业收入快速增长 Twilio 云通信成就大牛股
  5. php 单例模式的日志类,php单例模式实现日志处理类库
  6. mysql语法6_全面接触SQL语法(6)_mysql
  7. ITK:查找图像的更高导数
  8. 计算机基础知识整理大全_知识大全 | 物理选修35quot;波粒二象性quot;
  9. javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决
  10. Gradle_04_解决多项目同级依赖时找不到符号的异常
  11. IPMI 1:ipmi简介
  12. 系统语言在C盘什么地方,Win7桌面文件在C盘哪里?|Win7系统桌面文件在哪?
  13. iFunk翼S苏宁京东热卖进行中
  14. 关于使用系统定位持续后台定位的一点心得
  15. 周易六十四卦—水泽节卦
  16. CS和IP寄存器的作用及执行分析
  17. 敲7数字100以内c语言,敲七游戏数字表怎么玩?
  18. AI Security2-投毒攻击(Poisoning Attacks)
  19. React中文文档 7. 条件渲染
  20. GlobalWoZ: 面向全球通用的人机对话系统——快速构建多语对话能力初探

热门文章

  1. 今天情人节,程序员该如何绝地反击?
  2. MATLAB中table结构学习笔记01_table数据结构的创建
  3. mongodb修改数据语句_MongoDB安装及简单的'增删改查'语句
  4. 京东商城商品分类列表页面
  5. 微信直播相对一般直播平台怎么样?有哪些优势?
  6. 为什么快捷指令无法将媒体转换为文本_全知乎最全!iOS“捷径(快捷指令)”应用进阶教程 (附入门教程链接)...
  7. 【C语言刷题】青蛙跳台阶
  8. Android8.0 安装apk
  9. Nginx运维之一 反向代理、动静分离、负载均衡
  10. 关于python中如何导入pygame模块(超详细)