Java 通过post或get请求获取数据,java调用HttpRequest发起请求(记录)
作者:互联网
Java 通过post或get请求获取数据,java调用HttpRequest发起请求
依赖包:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;// **
1、get请求
public static void main(String[] args) {
//参数拼接
Map<String, String> data = new HashMap<>();
data.put("num", "xxxxxxx");
data.put("key", "xxxxxxx");
// 发起get请求 resp接收返回的数据
String resp = HttpRequest.get("https://www.kuaidi100.com/autonumber/auto").form(data).body();
System.out.println("Hello World :::" + resp);
JSONArray arr = JSON.parseArray(resp);
JSONObject obj1 = JSONObject.parseObject(arr.get(0).toString());
System.out.println("Hello World :::" + obj1.get("comCode"));
}
2、post请求
public static void main(String[] args) {
String param ="{\"com\":\"jd\",\"num\":\"JDVB10958953772\",\"to\":\"北京朝阳区国际金融大厦\"}";//com 快递公司标识,num:快递单号
String customer ="xxxxxxxxxx";//这是申请下来的公钥
String key = "xxxxxxx";//这是申请下来的私钥
String sign = SignUtils.querySign(param,key,customer);//md5加密
//参数拼接
HashMap<String, String> data = new HashMap<String, String>();
data.put("param",param);
data.put("sign",sign);
data.put("customer",customer);
// 发起post请求 resp1接收返回的数据
String resp1 = HttpRequest.post("https://poll.kuaidi100.com/poll/maptrack.do").form(data).body();
JSONObject obj = JSONObject.parseObject(resp1);
System.out.println("Hello World :::" + obj.get("data"));
}
标签:HttpRequest,请求,get,data,获取数据,post,com,String 来源: https://blog.csdn.net/weixin_42508580/article/details/121424326