|
|
@ -118,7 +118,7 @@ public class OpenHttp { |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
* @throws IOException |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public String request(String url, Map<String, String> form, Map<String, String> header, String method) throws IOException { |
|
|
|
public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException { |
|
|
|
Request.Builder requestBuilder = buildRequestBuilder(url, form, method); |
|
|
|
Request.Builder requestBuilder = buildRequestBuilder(url, form, method); |
|
|
|
// 添加header
|
|
|
|
// 添加header
|
|
|
|
addHeader(requestBuilder, header); |
|
|
|
addHeader(requestBuilder, header); |
|
|
@ -134,21 +134,21 @@ public class OpenHttp { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, String method) { |
|
|
|
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, HTTPMethod method) { |
|
|
|
switch (method) { |
|
|
|
switch (method) { |
|
|
|
case "get": |
|
|
|
case GET: |
|
|
|
return new Request.Builder() |
|
|
|
return new Request.Builder() |
|
|
|
.url(buildHttpUrl(url, form)) |
|
|
|
.url(buildHttpUrl(url, form)) |
|
|
|
.get(); |
|
|
|
.get(); |
|
|
|
case "head": |
|
|
|
case HEAD: |
|
|
|
return new Request.Builder() |
|
|
|
return new Request.Builder() |
|
|
|
.url(buildHttpUrl(url, form)) |
|
|
|
.url(buildHttpUrl(url, form)) |
|
|
|
.head(); |
|
|
|
.head(); |
|
|
|
case "put": |
|
|
|
case PUT: |
|
|
|
return new Request.Builder() |
|
|
|
return new Request.Builder() |
|
|
|
.url(url) |
|
|
|
.url(url) |
|
|
|
.put(buildFormBody(form)); |
|
|
|
.put(buildFormBody(form)); |
|
|
|
case "delete": |
|
|
|
case DELETE: |
|
|
|
return new Request.Builder() |
|
|
|
return new Request.Builder() |
|
|
|
.url(url) |
|
|
|
.url(url) |
|
|
|
.delete(buildFormBody(form)); |
|
|
|
.delete(buildFormBody(form)); |
|
|
@ -241,5 +241,22 @@ public class OpenHttp { |
|
|
|
this.httpClient = httpClient; |
|
|
|
this.httpClient = httpClient; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public enum HTTPMethod { |
|
|
|
|
|
|
|
GET, |
|
|
|
|
|
|
|
POST, |
|
|
|
|
|
|
|
PUT, |
|
|
|
|
|
|
|
HEAD, |
|
|
|
|
|
|
|
DELETE; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HTTPMethod() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String value() { |
|
|
|
|
|
|
|
return this.name(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static HTTPMethod fromValue(String v) { |
|
|
|
|
|
|
|
return valueOf(v.toUpperCase()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|