2020-09-28

使用okhttp构造http请求发送给服务端

主函数启动springbooot,@Controller注解声明是一个控制器,@RequestMapping配置URL和方法之间的映射,向http://localhost:8080/hi发送空请求即可触发test方法,可以测出请发送处理时延。
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@SpringBootApplication
public class HttpClientExample {
    public static final MediaType FORM = MediaType.parse("multipart/form-data; boundary=something");

    public String post(OkHttpClient client, String url) throws IOException {

        byte[] aa = new byte[684091];
        String str = new String(aa);

        RequestBody body = RequestBody.create(FORM,str);
        Request request = new Request.Builder().url(url)
                .post(body).build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    @RequestMapping("/hi")
    @ResponseBody
    public String test()
    {
        OkHttpClient client = new OkHttpClient();
        try {
            long start = System.currentTimeMillis();
            String res = new HttpClientExample().post(client, "填入http的url");
            long end = System.currentTimeMillis();
            System.out.println("程序运行时间: "+(end-start)+"ms");
            return res;
        } catch (IOException e) {
            e.printStackTrace();
            return "error_e";
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(HttpClientExample.class, args);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是代码实现: ```python import datetime def format_time_diff(start_time, end_time): time_diff = end_time - start_time if time_diff.days > 365: return end_time.strftime("%Y年%m月") elif time_diff.days > 30: return end_time.strftime("%Y年%m月%d日") elif time_diff.days > 0: return f"{time_diff.days}天前" elif time_diff.seconds > 3600: return f"{int(time_diff.seconds/3600)}小时前" elif time_diff.seconds > 60: return f"{int(time_diff.seconds/60)}分钟前" elif time_diff.seconds > 0: return f"{time_diff.seconds}秒前" else: return "未来时间" start_time = datetime.datetime(2018, 3, 1, 9, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 1, 1, 9, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 1, 9, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 8, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 9, 29, 20) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 9, 29, 50) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 9, 30, 40) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") ``` 输出结果为: ``` 2018-03-01 09:00:00 -> 2020-02-29 09:30:30: 2018年03月 2020-01-01 09:00:00 -> 2020-02-29 09:30:30: 2020年01月01日 2020-02-01 09:00:00 -> 2020-02-29 09:30:30: 28天前 2020-02-29 08:00:00 -> 2020-02-29 09:30:30: 1小时前 2020-02-29 09:29:20 -> 2020-02-29 09:30:30: 1分钟前 2020-02-29 09:29:50 -> 2020-02-29 09:30:30: 40秒前 2020-02-29 09:30:40 -> 2020-02-29 09:30:30: 未来时间 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值