forked from IoT-Technology/IoT-Technical-Guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b82e546
commit 0a35e8b
Showing
4 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...Guide-Gateway-Modbus/src/main/java/iot/technology/gateway/modbus/conveter/OkHttpUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package iot.technology.gateway.modbus.conveter; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import lombok.extern.slf4j.Slf4j; | ||
import okhttp3.FormBody; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author james mu | ||
* @date 2020/6/21 21:13 | ||
*/ | ||
@Slf4j | ||
public class OkHttpUtil { | ||
|
||
/** | ||
* Get请求 | ||
* @param url URL地址 | ||
* @return 返回结果 | ||
*/ | ||
public static String get(String url){ | ||
String result=null; | ||
try { | ||
OkHttpClient okHttpClient=new OkHttpClient(); | ||
Request request = new Request.Builder().url(url).build(); | ||
Response response = okHttpClient.newCall(request).execute(); | ||
result=response.body().string(); | ||
log.info("Get请求返回:{}",result); | ||
return result; | ||
}catch (Exception e){ | ||
log.error("OkHttp[Get]请求异常",e); | ||
return result; | ||
} | ||
} | ||
|
||
/** | ||
* Post请求 | ||
* @param url URL地址 | ||
* @param params 参数 | ||
* @return 返回结果 | ||
*/ | ||
public static String post(String url, Map<String,String> params){ | ||
String result=null; | ||
if (params==null){ | ||
params=new HashMap<String, String>(); | ||
} | ||
try { | ||
OkHttpClient okHttpClient=new OkHttpClient(); | ||
FormBody.Builder formBodyBuilder = new FormBody.Builder(); | ||
//添加参数 | ||
log.info("params:{}", JSON.toJSONString(params)); | ||
for (Map.Entry<String,String> map:params.entrySet()){ | ||
String key=map.getKey(); | ||
String value; | ||
if (map.getValue()==null){ | ||
value=""; | ||
}else{ | ||
value=map.getValue(); | ||
} | ||
formBodyBuilder.add(key,value); | ||
} | ||
FormBody formBody =formBodyBuilder.build(); | ||
Request request = new Request.Builder().url(url).post(formBody).build(); | ||
Response response = okHttpClient.newCall(request).execute(); | ||
result=response.body().string(); | ||
log.info("Post请求返回:{}",result); | ||
return result; | ||
}catch (Exception e){ | ||
log.error("OkHttp[Post]请求异常",e); | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters