get请求和post请求的特点

get请求

get请求的参数放在了url上,java中可以直接通过方法获取
缺点:
1、 不安全,一眼就可以看出请求的参数,不过可以通过加密解决
2、 请求的参数不易太长
优点:
比较好理解和操作,参数的值直接通过方法获取

Java代码发送get请求

    public String doGet(String url){
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    String result = "";
    try{
        //通过默认配置创建一个httpClient实例
        httpClient = HttpClients.createDefault();
        //创建httpGet远程连接实例
        HttpGet httpGet = new HttpGet(url);
        //httpGet.addHeader("Connection", "keep-alive");
        //设置请求头信息
        httpGet.addHeader("Accept", "application/json");
        //配置请求参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(35000) //设置连接主机服务超时时间
                .setConnectionRequestTimeout(35000)//设置请求超时时间
                .setSocketTimeout(60000)//设置数据读取超时时间
                .build();
        //为httpGet实例设置配置
        httpGet.setConfig(requestConfig);
        //执行get请求得到返回对象
        response = httpClient.execute(httpGet);
        //通过返回对象获取返回数据
        HttpEntity entity = response.getEntity();
        //通过EntityUtils中的toString方法将结果转换为字符串,后续根据需要处理对应的reponse code
        result = EntityUtils.toString(entity);
        System.out.println(result);

    }catch (ClientProtocolException e){
        e.printStackTrace();
    }catch (IOException ioe){
        ioe.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        //关闭资源
        if(response != null){
            try {
                response.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
        if(httpClient != null){
            try{
                httpClient.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
    }
    return result;
}

获取get请求的参数

request.getParameter("appCode");

post请求

post请求的参数放在了请求体中,无法通过方法直接获取,需要通过io流获取
缺点:
1、 对新手不太友好,初次接触可能不知道怎么获取
2、 使用java代码发送post请求也比较麻烦,无法做到跟get请求一样直接拼接
优点:
安全,传输的请求参数长度不限

Java代码发送post请求

public String doPost(String url){
    //创建httpClient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    String result = "";
    try{
        //创建http请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        //创建请求内容
        String jsonStr = "{\"qry_by\":\"name\", \"name\":\"Tim\"}";
        StringEntity entity = new StringEntity(jsonStr);
        httpPost.setEntity(entity);
        response = httpClient.execute(httpPost);
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        //关闭资源
        if(response != null){
            try {
                response.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
        if(httpClient != null){
            try{
                httpClient.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
    }
    return result;
}

获取post请求的参数

    BufferedReader reader = request.getReader();
    StringBuffer stringBuffer = new StringBuffer();
    String read = "";
    while ((read = reader.readLine()).length() > 0){
        stringBuffer.append(read);
    }
    System.out.println(stringBuffer);
    reader.close();

参考

具体方式可以查看该链接
https://www.jianshu.com/p/117264481886

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值