Skip to content

Commit 104a091

Browse files
author
xingqisheng
committed
HTTPUTIL
1 parent 0fbb930 commit 104a091

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/main/java/com/devil/utils/HttpUtil.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class HttpUtil {
4646
private static final int READ_TIMEOUT = 2 * 60 * 1000;
4747
private static final Charset CHARSET = StandardCharsets.UTF_8;
4848

49-
public static String getStr(String url, String... params) throws HttpException, IOException {
49+
public static String getStr(String url, String... params) throws HttpException {
5050
HttpGet get = buildGet(url, params);
5151
return httpStr(get);
5252
}
@@ -56,7 +56,7 @@ public static String getStr(String url, String... params) throws HttpException,
5656
// return httpInputStream(get);
5757
// }
5858

59-
public static String postStr(String url, String... params) throws HttpException, IOException {
59+
public static String postStr(String url, String... params) throws HttpException {
6060
HttpPost post = buildPost(url, params);
6161
return httpStr(post);
6262
}
@@ -72,7 +72,7 @@ public static File download(String path, String url, String... params) throws Ht
7272
return httpFile(get, path);
7373
}
7474

75-
public static String submitFile(String url, String name, File f,String... params) throws HttpException, IOException {
75+
public static String submitFile(String url, String name, File f, String... params) throws HttpException {
7676
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
7777
// builder.setCharset(Charset.forName("uft-8"));//设置请求的编码格式
7878
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);// 设置浏览器兼容模式
@@ -88,16 +88,17 @@ public static String submitFile(String url, String name, File f,String... params
8888
post.setEntity(builder.build());
8989
return httpStr(post);
9090
}
91-
92-
public static String postStream(String url, String body) throws HttpException, IOException {
91+
92+
public static String postStream(String url, String body) throws HttpException {
9393
HttpPost post = new HttpPost(url);
9494
StringEntity entiry = new StringEntity(body, "UTF-8");
9595
post.setEntity(entiry);
9696
post.setHeader("Content-Type", "text/xml;charset=utf-8");
9797
return httpStr(post);
9898
}
99+
99100
// //////////////////////////////////////////////////////////////
100-
private static String httpStr(HttpUriRequest req) throws IOException {
101+
private static String httpStr(HttpUriRequest req) throws HttpException {
101102
CloseableHttpResponse resp = null;
102103
try {
103104
// and then from inside some thread executing a method
@@ -106,7 +107,7 @@ private static String httpStr(HttpUriRequest req) throws IOException {
106107
resp = client.execute(req);
107108
if (resp == null || resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
108109
req.abort();
109-
throw new IOException("url:" + req.getURI() + "->\n reponse:" + resp.getStatusLine().toString());
110+
throw new HttpException("url:" + req.getURI() + "->\n reponse:" + resp.getStatusLine().toString());
110111
}
111112

112113
HttpEntity entity = resp.getEntity();
@@ -119,6 +120,8 @@ private static String httpStr(HttpUriRequest req) throws IOException {
119120
String result = getString(stream, cs);
120121
EntityUtils.consume(entity);
121122
return result;
123+
} catch (IOException e) {
124+
throw new HttpException("HTTP出错", e);
122125
} finally {
123126
if (resp != null) {
124127
try {
@@ -187,7 +190,13 @@ private static File httpFile(HttpUriRequest req, String path) throws IOException
187190

188191
private static HttpGet buildGet(String url, String... params) {
189192
if (params != null && params.length > 0) {
190-
StringBuilder sb = new StringBuilder(url).append("?");
193+
StringBuilder sb = new StringBuilder(url);
194+
if (!url.contains("?")) {
195+
sb.append("?");
196+
}
197+
if (!url.endsWith("?") && !url.endsWith("&")) {
198+
sb.append("&");
199+
}
191200
for (int i = 0; i < params.length - 1; i += 2) {
192201
if (!CommUtil.isEmpty(params[i + 1])) {
193202
String value = params[i + 1];

0 commit comments

Comments
 (0)