Spring Boot OkHttp

1、pom.xml 添加依赖

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
</dependency>

2、OkHttpController.java

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import okhttp3.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("okhttp")
public class OkHttpController {

    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static final MediaType XML = MediaType.parse("application/xml; charset=utf-8");

    private OkHttpClient client = new OkHttpClient();

    /**
     * get请求
      * @return
     */
    @RequestMapping("get")
    public String Get(){
        String url = "http://localhost:36247/weatherforecast/string";
        Request request = new Request.Builder()
                .url(url)
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string();
        }catch (Exception e){
            throw  new RuntimeException("HTTP GET同步请求失败 URL:"+url,e);
        }
    }

    /**
     * get请求
     * @return
     */
    @RequestMapping("getUser")
    public String GetUser(){
        String url = "http://localhost:36247/weatherforecast/getuser?name=admin_get&password=123456_get";
        Request request = new Request.Builder()
                .url(url)
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string();
        }catch (Exception e){
            throw  new RuntimeException("HTTP GET同步请求失败 URL:"+url,e);
        }
    }

    /**
     * post 请求
     * FormData 参数
     * @return
     */
    @RequestMapping("postForm")
    public String PostForm(){
        String url = "http://localhost:36247/weatherforecast/form";

        FormBody.Builder form = new FormBody.Builder();
        form.add("name","admin_form");
        form.add("password","123456_form");

        Request request = new Request.Builder()
                .url(url)
                .post(form.build())
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string();
        }catch (Exception e){
            throw  new RuntimeException("HTTP GET同步请求失败 URL:"+url,e);
        }
    }

    /**
     * post 请求
     * JSON 参数
     * @return
     */
    @RequestMapping("postJson")
    public String PostJson(){
        String url = "http://localhost:36247/weatherforecast";

        /**
         * OK
         */
        JSONObject json = new JSONObject();
        json.put("name","admin_json");
        json.put("password","123456_json");

        /**
         * OK
         */
        Map<String,Object> map = new HashMap<>();
        map.put("name","admin_map");
        map.put("password","123456_map");

        RequestBody requestBody = RequestBody.create(JSON,String.valueOf(json));
        //RequestBody requestBody = RequestBody.create(JSON,new Gson().toJson(map));

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string();
        }catch (Exception e){
            throw  new RuntimeException("HTTP POST请求失败 URL:"+url,e);
        }
    }
}

*
*
*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值