get 请求
//创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS) // 超时设置
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
final Request request = new Request.Builder()
.url("http://XXXX.com")
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36")
.addHeader("Host", "XXXX.com")
.get()
.build();
Call call = okHttpClient.newCall(request);
//异步操作
call.enqueue(new Callback() {
//请求错误回调方法
@Override
public void onFailure(Call call, IOException e) {
System.out.println("连接失败");
//异步请求(非主线程)
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code() == 200) {
System.out.println(response.body().string());
});
post 请求 -- form表单提交
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
//post方式提交的数据
FormBody formBody = new FormBody.Builder()
.add("t", "test")
.build();
final Request request = new Request.Builder()
.url("http://XXX.com")//请求的url
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36")
.addHeader("Host", "XXXX.com")
.post(formBody)
.build();
//创建/Call
Call call = okHttpClient.newCall(request);
//加入队列 异步操作
call.enqueue(new Callback() {
//请求错误回调方法
@Override
public void onFailure(Call call, IOException e) {
System.out.println("连接失败");
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code() == 200) {
System.out.println(response.body().string());
});
post 请求 -- json格式提交
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
JSONObject json = new JSONObject();
try {
json.put("t", "test");
} catch (JSONException e) {
e.printStackTrace();
//MediaType 设置Content-Type 标头中包含的媒体类型值
RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), json);
Request request = new Request.Builder()
.url("http://XXX.com")//请求的url
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36")
.addHeader("Host", "XXXX.com")
.post(requestBody)
.build();
//创建/Call
Call call = okHttpClient.newCall(request);
//加入队列 异步操作
call.enqueue(new Callback() {
//请求错误回调方法
@Override
public void onFailure(Call call, IOException e) {
System.out.println("连接失败");
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.body().string());
【Gin】gin json渲染,获取querystring参数,获取form参数,获取path参数,参数绑定
访问:http://localhost:9090/user?username=dahe&password=admin
控制台输出:main.UserInfo{Username:“dahe”, Password:“admin”}
ShouldBind会按照下面的顺序解析请求中的数据完成绑定:
如果是 GET 请求,只使用 Form 绑定引擎(query)。
如果是 POST 请求,首先检查 content-type 是否为 JSON 或 XML,然后再使用 Form(form-data)。
dahezhiquan
1.把URL及info的对像这两个参数发给login函数把结构体对象转换成json,
2.用POST方法提交JSON的数据到服务器上
3.通过调用Client.Do方法得到服务器的响应response的JSON
4.把服务器响应回来的JSON解析成结构体对象来存储相应的信息
5.调用解析JSON的结构体对象的各属性得到相应的信息
暗夜编码人