java调用http接口没有返回值_java使用Http调用第三方接口,输入链接,获取返回值...

1 importorg.slf4j.Logger;2 importorg.slf4j.LoggerFactory;3 import java.io.*;4 importjava.net.HttpURLConnection;5 importjava.net.URL;6 importjava.util.Map;7 importjava.util.Set;8

9 /**

10 *@authorwusiwee11 * @date 2019/12/17 16:5012 */

13 public classHttpUtil {14

15 private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);16

17

18 /**

19 * 支持的Http method20 */

21 private static enumHttpMethod {22 POST, DELETE, GET, PUT, HEAD23 }24

25 @SuppressWarnings({"unchecked", "rawtypes"})26 private static String invokeUrl(String url, Map params, Map headers, int connectTimeout, intreadTimeout, String encoding, HttpMethod method) {27 //构造请求参数字符串

28 StringBuilder paramsStr = null;29 if (params != null) {30 paramsStr = newStringBuilder();31 Set entries =params.entrySet();32 for(Map.Entry entry : entries) {33 String value = (entry.getValue() != null) ? (String.valueOf(entry.getValue())) : "";34 paramsStr.append(entry.getKey() + "=" + value + "&");35 }36 //只有POST方法才能通过OutputStream(即form的形式)提交参数

37 if (method !=HttpMethod.POST) {38 url += "?" +paramsStr.toString();39 }40 }41

42 URL uUrl = null;43 HttpURLConnection conn = null;44 BufferedWriter out = null;45 BufferedReader in = null;46 try{47 //创建和初始化连接

48 uUrl = newURL(url);49 conn =(HttpURLConnection) uUrl.openConnection();50 conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");51 conn.setRequestMethod(method.toString());52 conn.setDoOutput(true);53 conn.setDoInput(true);54 //设置连接超时时间

55 conn.setConnectTimeout(connectTimeout);56 //设置读取超时时间

57 conn.setReadTimeout(readTimeout);58 //指定请求header参数

59 if (heade

你可能感兴趣的:(java调用http接口没有返回值_java使用Http调用第三方接口,输入链接,获取返回值...)