zoukankan      html  css  js  c++  java
  • HttpRequest,HTTP的请求利器

    项目地址: https://github.com/kevinsawicki/http-request

    1、发送一个get请求,获取响应码,够方便了吧。
    int code = HttpRequest.get("http://google.com").code();
    2、加个请求参数呢?可以直接加在get方法里,选择是否进行编码,不习惯的还可以用Map传参哦。
    HttpRequest request = HttpRequest.get("http://google.com", true, 'q', "baseball gloves", "size", 100);
    3、发一个带文件的POST请求
    HttpRequest request = HttpRequest.post("url”);
    request.header("Content-Type", "multipart/form-data;boundary=AaB03x");
    request.part("imagefile", "test.log", "image/jpeg", new File("d:/test/test.jpg"));
    4、再发一个带Form的POST
            Map<String, String> data = new HashMap<String, String>();
            data.put("user", "A User");
            data.put("state", "CA");
            HttpRequest request = HttpRequest.post(url).form(data);
    5、发送带JSON的POST

            JsonObject jsonContent = new JsonObject();

            jsonContent.addProperty("content", msgBody);

            JsonObject jsonData = new JsonObject();

            jsonData.add("data", jsonContent);

            jsonData.addProperty("subtype", subType);

    HttpRequest httpRequest = HttpRequest.post(url).acceptJson();

            httpRequest.send(jsonData.toString());

            int code = httpRequest.code();

            String body = httpRequest.body();

    这个开源实现,最大的特点是基于URLConnection实现,不依赖HttpClient。
    整个项目的实现只有一个Java类文件,有兴趣的可以自己看哦。

  • 相关阅读:
    第一章 Java Collections Framework总览
    MySQL常用sql语句
    static关键字
    Mysql常用索引及优化
    JDK安装及环境变量配置
    破解IntelliJ IDEA 2017
    Spring自我总结
    Java中根据字节截取字符串
    外键的主要作用:保持数据的一致性、完整性
    使用SSI框架写的简单Demo(查询模块)
  • 原文地址:https://www.cnblogs.com/codekey/p/4532735.html
Copyright © 2011-2022 走看看