springboot发送http请求

springboot中实现http请求调用api

  1. 创建发送http请求service层

    import org.springframework.http.*;
    import org.springframework.stereotype.Service;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @Author 冯战魁
     * @Date 2018/1/23 下午5:43
     */
    @Service
    public class HttpClient {
        public String client(String url, HttpMethod method, MultiValueMap<String, String> params){
            RestTemplate client = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //  请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
            //  执行HTTP请求
            ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
            return response.getBody();
        }
    }
  2. 添加本地测试url localhost:8080/hello
    import com.example.demo.service.HttpClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.*;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @Author 冯战魁
     * @Date 2018/1/8 上午11:17
     */
    @RestController
    public class HelloController {
        @Autowired
        HttpClient httpClient;
        @RequestMapping("/hello")
        public String hello(){
            //api url地址
            String url = "http://xxxx";
            //post请求
            HttpMethod method =HttpMethod.POST;
            // 封装参数,千万不要替换为Map与HashMap,否则参数无法传递
            MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
            params.add("access_token", "xxxxx");
            //发送http请求并返回结果
            return httpClient.client(url,method,params);
        }
    }
  3. 访问localhost:8080/hello查看调用结果

    curl http://localhost:8080/hello

原文地址:http://blog.51cto.com/fengzhankui/2064327

时间: 2024-08-08 09:49:38

springboot发送http请求的相关文章

springboot成神之——websocket发送和请求消息

本文介绍如何使用websocket发送和请求消息 项目目录 依赖 DemoApplication MessageModel WebConfig WebSocketConfig HttpHandshakeInterceptor WebSocketEventListener WebSocketController 前端测试 本文介绍如何使用websocket发送和请求消息 项目目录 依赖 <dependency> <groupId>org.springframework.boot<

Ajax发送post请求

//创建Ajax对象(兼容处理) function createXHR() { var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if(window.ActiveXObject) { xhr = new ActiveXObject('Microsoft.XMLHTTP'); } return xhr; } //发送post请求 function request(){ xhr.open('P

每天一个linux命令13之curl发送http请求

一.get请求 curl "http://www.baidu.com"  如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http://www.baidu.com"  显示全部信息 curl -l "http://www.baidu.com" 只显示头部信息 curl -v "http://www.baidu.com" 显示get请求全过程解析 wget "http://www.ba

jmeter 发送http请求,并把获取到的请求的订单信息保存到文件中

有一个任务,需要频繁发送订单请求,并分析订单请求中有没有存在重复订单号,思路是用jmeter 发送http请求,使用正则表达式获取到订单号,并把订单号和线程号作为参数提供给java请求,在java请求中把订单号写到包括有线程号的命名文件中.完成出来的样子是这样的 步骤如下: 1.Jmeter 发送http请求 1) 新建HTTP信息头管理器 2) 新建HTTP请求 2,在下单过后返回部分信息如下:"msg":"下单成功","result":&qu

nodejs向远程服务器发送post请求----融云Web SDK/客户端获取token

最近要用到一个叫融云的及时通讯的SDK,在获取token这个步骤的时候有点卡顿,以防以后碰到类似的问题,再此记录一下. 客户端通过融云 SDK 每次连接服务器时,都需要向服务器提供 Token,以便验证身份,流程如下: 流程如下: 1.客户端获取用户id,并向服务器请求token(注意这里的服务器不是融云的服务器,而是客户端的服务端) 2.客户端的服务端接收到token请求后,向融云的服务器请求token 3.融云服务器接受到token请求,返回token给客户端的服务端. 4.客户端的服务端接

socket发送http请求

转自:思齐-socket发送http请求 socket方式: $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>20, "usec"=>0)); socket_connect($socket, 'www.baidu.com', 80); //里面的换行代表 \r

夺命雷公狗jquery---55---Ajax的高级实现,模拟发送get请求

jQuery.get(url,[data],[callback]) 或 $.get jQuery.post(url,[data],[callback]) 或 $.post 参数说明: url:请求的url页面 [data]:发送Ajax时传递的参数,要求格式为json对象,如果没有可以不写,直接写第三个参数即可 [callback]:当Ajax状态码为4且响应状态码为200时所触发的回调函数 <!DOCTYPE html> <html> <head> <meta

Android面向HTTP协议发送get请求

/** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ public static String getRequest(String username, String password) { try { String path = "http://172.22.64.156:8080/0001AndroidWebService/LoginServlet?use

解决浏览器跨域限制发送ajax请求

一.什么是浏览器跨域限制?本质是什么? 所谓浏览器跨域限制,其实是为了数据安全的考虑由Netscape提出来限制浏览器跨域访问数据的策略,这是一中约定,正式叫法为浏览器同源策略,目前已经在大多数浏览器中支持. 本质上,所谓浏览器同源策略即:不允许浏览器访问跨域的Cookie,ajax请求跨域接口等.也就是说,凡是访问与自己不在相同域的数据或接口时,浏览器都是不允许的. 最常见的例子:对于前后端完全分离的Web项目,前端页面通过rest接口访问数据时,会出现如下问题: 不允许发送POST请求:在发