提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.util; |
E |
2 |
|
|
3 |
import java.io.BufferedReader; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.io.InputStreamReader; |
|
6 |
import java.io.OutputStream; |
|
7 |
import java.io.OutputStreamWriter; |
|
8 |
import java.net.HttpURLConnection; |
|
9 |
import java.net.URL; |
|
10 |
import java.net.URLEncoder; |
|
11 |
import java.util.ArrayList; |
|
12 |
import java.util.List; |
|
13 |
import java.util.Map; |
|
14 |
|
|
15 |
public class RequestMethod { |
|
16 |
|
|
17 |
/** |
|
18 |
* 发送Post请求 |
|
19 |
* @param path 请求路径 |
|
20 |
* @param params 请求参数 |
|
21 |
* @param encoding 编码 |
|
22 |
* @return 服务器端内容 |
|
23 |
*/ |
|
24 |
public static String sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{ |
|
25 |
StringBuilder data = new StringBuilder(); |
|
26 |
if(params!=null && !params.isEmpty()){ |
|
27 |
for(Map.Entry<String, String> entry : params.entrySet()){ |
|
28 |
data.append(entry.getKey()).append("="); |
|
29 |
data.append(URLEncoder.encode(entry.getValue(), encoding)); |
|
30 |
data.append("&"); |
|
31 |
} |
|
32 |
data.deleteCharAt(data.length() - 1); |
|
33 |
} |
|
34 |
byte[] entity = data.toString().getBytes();//生成实体数据 |
|
35 |
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); |
|
36 |
conn.setConnectTimeout(5000); |
|
37 |
conn.setRequestMethod("POST"); |
|
38 |
conn.setDoOutput(true);//允许对外输出数据 |
|
39 |
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
|
40 |
conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); |
|
41 |
OutputStream outStream = conn.getOutputStream(); |
|
42 |
outStream.write(entity); |
|
43 |
if(conn.getResponseCode() == 200){ |
|
44 |
return StreamUtils.InputStreamTOString(conn.getInputStream()) ; |
|
45 |
} |
|
46 |
return null; |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* 发送Post请求 |
|
51 |
* @param path 请求路径 |
|
52 |
* @param content 发送内容 |
|
53 |
* @param encoding 编码 |
|
54 |
* @return 服务器端内容 |
|
55 |
*/ |
|
56 |
public static String sendPOSTRequest(String path, String content, String encoding){ |
|
57 |
//byte[] entity = content.toString().getBytes();//生成实体数据 |
|
58 |
try{ |
|
59 |
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); |
|
60 |
conn.setConnectTimeout(5000); |
|
61 |
conn.setRequestMethod("POST"); |
|
62 |
conn.setDoOutput(true);//允许对外输出数据 |
|
63 |
conn.setDoInput(true); |
|
64 |
conn.setUseCaches(false); |
|
65 |
conn.setRequestProperty("Charset", "utf-8"); |
|
66 |
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
|
67 |
//conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); |
|
68 |
OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8"); |
|
69 |
outStream.write(content); |
|
70 |
outStream.flush(); |
|
71 |
if(conn.getResponseCode() == 200){ |
|
72 |
return StreamUtils.InputStreamTOString(conn.getInputStream()); |
|
73 |
} |
|
74 |
}catch(Exception e){ |
|
75 |
e.printStackTrace(); |
|
76 |
} |
|
77 |
return null; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* 发送Post请求 |
|
82 |
* @param path 请求路径 |
|
83 |
* @param entity 发送内容 |
|
84 |
* @param encoding 编码 |
|
85 |
* @return 服务器端内容 |
|
86 |
*/ |
|
87 |
public static String sendPOSTRequest(String path, byte[] entity, String encoding) throws Exception{ |
|
88 |
System.out.println("entity:" + entity.length); |
|
89 |
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); |
|
90 |
conn.setConnectTimeout(5000); |
|
91 |
conn.setRequestMethod("POST"); |
|
92 |
conn.setDoOutput(true);//允许对外输出数据 |
|
93 |
conn.setRequestProperty("Content-Type", "application/octet-stream"); |
|
94 |
conn.setRequestProperty("Accept", "application/octet-stream"); |
|
95 |
conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); |
|
96 |
OutputStream outStream = conn.getOutputStream(); |
|
97 |
outStream.write(entity); |
|
98 |
if(conn.getResponseCode() == 200){ |
|
99 |
return StreamUtils.InputStreamTOString(conn.getInputStream()); |
|
100 |
} |
|
101 |
return null; |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* 发送GET请求 |
|
106 |
* @param path 请求路径 |
|
107 |
* @param params 请求参数 |
|
108 |
* @return 服务器端内容 |
|
109 |
*/ |
|
110 |
public static String sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{ |
|
111 |
// http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90 |
|
112 |
StringBuilder url = new StringBuilder(path); |
|
113 |
if(params!=null){ |
|
114 |
url.append("?"); |
|
115 |
for(Map.Entry<String, String> entry : params.entrySet()){ |
|
116 |
url.append(entry.getKey()).append("="); |
|
117 |
url.append(URLEncoder.encode(entry.getValue(), ecoding)); |
|
118 |
url.append("&"); |
|
119 |
} |
|
120 |
url.deleteCharAt(url.length() - 1); |
|
121 |
} |
|
122 |
HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection(); |
|
123 |
conn.setConnectTimeout(5000); |
|
124 |
conn.setRequestMethod("GET"); |
|
125 |
if(conn.getResponseCode() == 200){ |
|
126 |
return StreamUtils.InputStreamTOString(conn.getInputStream()) ; |
|
127 |
} |
|
128 |
return null; |
|
129 |
} |
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
public static String msg(InputStream in) throws Exception{ |
|
134 |
BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
|
135 |
String line; |
|
136 |
List<String> lines = new ArrayList<String>(); |
|
137 |
while((line = br.readLine())!=null){ |
|
138 |
lines.add(line); |
|
139 |
} |
|
140 |
if(lines.size()>0){ |
|
141 |
String f = lines.get(0); |
|
142 |
if(f.indexOf("200")!=-1){ |
|
143 |
String e = lines.get(lines.size()-1); |
|
144 |
return e; |
|
145 |
}else{ |
|
146 |
return null; |
|
147 |
} |
|
148 |
}else{ |
|
149 |
return null; |
|
150 |
} |
|
151 |
} |
|
152 |
} |