Skip to content

Commit

Permalink
完善HttpUtil工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
fengwenyi committed Aug 15, 2018
1 parent f754f05 commit 26a9d3c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
84 changes: 77 additions & 7 deletions src/main/java/com/fengwenyi/javalib/util/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class HttpUtil {

/**
* 通过 get 方式请求数据
* @param url {URL} / {URL+Param}
* @param url {URL}
* @param headers 请求的属性,也叫请求头
* @param params 在参数过多的时候,可能你更喜欢用 Map 来进行保存你的参数
* @return {URL}返回的数据,类型是 String
Expand All @@ -29,8 +29,38 @@ public static String get(String url,
Map<String, String> params) throws IOException {

// 参数
if (params != null) {
if (params != null && !params.isEmpty())
url += "?" + Utils.getUrlParamsByMap(params);

HttpURLConnection httpUrlConn = httpUrlConn(url);
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod(Constant.RequestMethodGet);

// header
if (headers != null && !headers.isEmpty())
header(headers, httpUrlConn);

httpUrlConn.connect();

return readIO(httpUrlConn);
}

/**
* 通过 get 方式请求数据
* @param url {URL}
* @param headers 请求的属性,也叫请求头
* @param params 在参数过多的时候,可能你更喜欢用 Map 来进行保存你的参数
* @return {URL}返回的数据,类型是 String
* @throws IOException IO异常:给定的 URL 不正确,或者其他原因,导致服务法法找到
* 或是在向服务器上传数据时,当然还有可能在读取服务返回的数据时
*/
public static String get(String url,
Map<String, String> headers,
String params) throws IOException {

// 参数
if (StringUtil.isNotEmpty(params)) {
url += "?" + params;
}

HttpURLConnection httpUrlConn = httpUrlConn(url);
Expand All @@ -47,6 +77,41 @@ public static String get(String url,
return readIO(httpUrlConn);
}

/**
* 通过 get 方式请求数据
* @param url {URL} / {URL+Param}
* @return {URL}返回的数据,类型是 String
* @throws IOException IO异常:给定的 URL 不正确,或者其他原因,导致服务法法找到
* 或是在向服务器上传数据时,当然还有可能在读取服务返回的数据时
*/
public static String get(String url) throws IOException {
return HttpUtil.get(url, null, "");
}

/**
* 通过 get 方式请求数据
* @param url {URL} / {URL+Param}
* @param headers 请求的属性,也叫请求头
* @return {URL}返回的数据,类型是 String
* @throws IOException IO异常:给定的 URL 不正确,或者其他原因,导致服务法法找到
* 或是在向服务器上传数据时,当然还有可能在读取服务返回的数据时
*/
public static String get(String url, Map<String, String> headers) throws IOException {
return HttpUtil.get(url, headers, "");
}

/**
* 通过 get 方式请求数据
* @param url {URL}
* @param params 在参数过多的时候,可能你更喜欢用 Map 来进行保存你的参数
* @return {URL}返回的数据,类型是 String
* @throws IOException IO异常:给定的 URL 不正确,或者其他原因,导致服务法法找到
* 或是在向服务器上传数据时,当然还有可能在读取服务返回的数据时
*/
public static String get(String url, String params) throws IOException {
return HttpUtil.get(url, null, params);
}

/**
* 通过 post 方式请求数据
* @param url {URL}
Expand Down Expand Up @@ -155,8 +220,12 @@ public static String delete(String url,
return readIO(httpUrlConn);
}

//-----------------------------------------------------------------------------------------------------private start

// header代码段
private static void header(Map<String, String> header, HttpURLConnection httpUrlConn) {
if (header == null)
return;
if (!header.isEmpty()) {
for (Map.Entry<String, String> map : header.entrySet()) {
String headerKey = map.getKey();
Expand Down Expand Up @@ -194,10 +263,8 @@ private static String readIO(HttpURLConnection httpUrlConn) throws IOException {
return stringBuilder.toString();
}

/**
* DataOutputStream
*/
private String doPostByUrl(String uri, String param) throws IOException {
// DataOutputStream
/*private String doPostByUrl(String uri, String param) throws IOException {
HttpURLConnection connection;
URL url = new URL(uri);
Expand Down Expand Up @@ -230,5 +297,8 @@ private String doPostByUrl(String uri, String param) throws IOException {
connection.disconnect();
return buffer.toString();
}
}*/

//-----------------------------------------------------------------------------------------------------private end

}
15 changes: 15 additions & 0 deletions src/main/java/com/fengwenyi/javalib/util/HttpUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fengwenyi.javalib.util;

/**
* Http工具类
* <p>Http工具类,为系统提供通用Http访问操作方法:</p>
* <ul>
* <li>get</li>
* <li>post</li>
* <li>put</li>
* <li>delete</li>
* </ul>
* @author Wenyi Feng
*/
public class HttpUtils {
}

0 comments on commit 26a9d3c

Please sign in to comment.