一:概述 1.准备项目 在这里需要使用两个项目进行实验。 调用的项目: 需要添加使用okhttp包,使用端口8090. 被调用的项目: 使用端口8080 是一个普通的项目即可。 二:程序GET 1.get的使
一:概述
1.准备项目
在这里需要使用两个项目进行实验。
调用的项目:
需要添加使用okhttp包,使用端口8090.
被调用的项目:
使用端口8080
是一个普通的项目即可。
二:程序GET
1.get的使用
被调用程序
package com.jun.web2forokhttp.okhttp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class GetReq {
@GetMapping("/ok/getInfo")
public Map getInfo(@RequestParam("type") String type){
Map map =new HashMap();
if("1".equals(type)){
map.put("1","aa");
map.put("2","bb");
}else {
map.put("3","cc");
map.put("4","dd");
}
return map;
}
}
2.调用程序
package com.jun.web.okhttp;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class GetHttp {
public static void main(String[] args) {
withoutHeader();
}
public static void withoutHeader(){
String url="http://localhost:8080/ok/getInfo?type=18";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
try{
Response response = call.execute();
System.out.println("get="+response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果:
3.
