http请求(一) ---- 工具
2
import
java.io.ByteArrayOutputStream;
3
import
java.io.DataOutputStream;
4
import
java.io.InputStream;
5
import
java.net.HttpURLConnection;
6
import
java.net.URL;
7
import
java.net.URLEncoder;
8
import
java.util.Map;
9
import
android.util.Log;
10
11
public
class
NetUtil {
12
13
private
static
final
String TAG
=
"
NetUtil
"
;
14
private
static
final
int
RESPONSE_OK
=
200
;
15
16
public
static
InputStream sendPostRequest(String urlPath,
17
Map
<
String, String
>
params, String encoding)
throws
Exception {
18
//
String param = "method=save&id=24&name="
19
//
+ URLEncoder.encode("大圆", "UTF-8");
20
StringBuilder sb
=
new
StringBuilder();
21
for
(Map.Entry
<
String, String
>
entry : params.entrySet()) {
22
sb.append(entry.getKey()).append(
"
=
"
)
23
.append(URLEncoder.encode(entry.getValue(), encoding))
24
.append(
"
&
"
);
25
}
26
sb.deleteCharAt(sb.lastIndexOf(
"
&
"
));
27
byte
[] data
=
sb.toString().getBytes();
28
URL url
=
new
URL(urlPath);
29
HttpURLConnection conn
=
(HttpURLConnection) url.openConnection();
30
conn.setRequestMethod(
"
POST
"
);
31
conn.setReadTimeout(
5
*
1000
);
32
conn.setDoOutput(
true
);
//
发送POST请求, 必须设置允许输出
33
conn.setUseCaches(
false
);
34
conn.setRequestProperty(
"
Connection
"
,
"
Keep-Alive
"
);
//
维持长链接
35
conn.setRequestProperty(
"
Charset
"
,
"
UTF-8
"
);
36
//
设置输入参数的大小,把参数转化为字节数组
37
conn.setRequestProperty(
"
Content-Length
"
, String.valueOf(data.length));
38
//
设置数据类型
39
conn.setRequestProperty(
"
Content-Type
"
,
40
"
application/x-www-form-urlencoded
"
);
41
42
DataOutputStream outStream
=
new
DataOutputStream(
43
conn.getOutputStream());
44
outStream.write(data);
45
outStream.flush();
46
outStream.close();
47
if
(conn.getResponseCode()
==
RESPONSE_OK) {
48
return
conn.getInputStream();
49
}
50
return
null
;
51
}
52
53
/*
54
* 得到http返回的输入流,并且转化成String
55
*/
56
public
static
String getTextContent(String urlPath, String encoding)
57
throws
Exception {
58
URL url
=
new
URL(urlPath);
59
HttpURLConnection conn
=
(HttpURLConnection) url.openConnection();
60
conn.setRequestMethod(
"
GET
"
);
61
conn.setReadTimeout(
5
*
1000
);
62
if
(conn.getResponseCode()
==
RESPONSE_OK) {
63
InputStream inStream
=
conn.getInputStream();
64
byte
[] data
=
readStream(inStream);
65
System.out.println(
new
String(data, encoding));
66
return
new
String(data, encoding);
67
}
68
return
null
;
69
}
70
71
//
读取数据
72
public
static
byte
[] readStream(InputStream inStream)
throws
Exception {
73
ByteArrayOutputStream outStream
=
new
ByteArrayOutputStream();
74
byte
[] buffer
=
new
byte
[
2048
];
75
int
length
=
-
1
;
76
while
((length
=
(inStream.read(buffer)))
!=
-
1
) {
77
outStream.write(buffer,
0
, length);
78
}
79
outStream.close();
80
return
outStream.toByteArray();
81
}
82
83
//
直接返回http得到的输入流
84
public
static
InputStream getStreamContent(String urlPath, String encoding)
85
throws
Exception {
86
InputStream inStream
=
null
;
87
URL url
=
new
URL(urlPath);
88
HttpURLConnection conn
=
(HttpURLConnection) url.openConnection();
89
conn.setRequestMethod(
"
GET
"
);
90
conn.setReadTimeout(
5
*
1000
);
91
if
(conn.getResponseCode()
==
RESPONSE_OK) {
92
inStream
=
conn.getInputStream();
93
}
94
return
inStream;
95
}
96
97
public
static
void
print(String tag, String msg) {
98
Log.d(tag, msg);
99
}
100
}
101
分享到:
相关推荐
Java发送http请求、https请求,post和get都支持
Java实现的Https请求工具类。 https的java工具类 Https
Java发 Https请求工具类 支持SSL。 自定义请求方式 GET POST... 微信自用, 发Https请求工具类,支持SSL SSL Https
用于发送http请求的一个简单的工具类,实现比较简单,可以参考。
这是一个java发送get、post请求,并得到返回结果的工具类。
RESTClient是一个用于测试RESTful Web services的Java客户端。 RESTClient是一款十分有用的http请求测试工具,是restful接口测试的必要工具。
WDSsoft的一款免费源代码 JCT 1.0,它是一个Java加密解密常用工具包。 Java局域网通信——飞鸽传书源代码 28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB...
JAVA http请求工具类
java 发送https get请求工具类
http请求工具类-HttpClientUtils 还需要HttpClientResult类
主要介绍了java模拟发送form-data的请求方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
java网络请求工具类 HttpURLConnection post请求工具类HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式
Java Http 工具类,和Https工具类,自动识别Https走SSL协议
Https请求工具类,Java实现http请求,https请求
NULL 博文链接:https://fanshuyao.iteye.com/blog/2436277
压缩包里包含http和https两种请求,第一个文件是http请求包含post和get两种方式,剩下的是https的请求,最后一个文件里包含https的post和get两种方式。中间的两个是post 和 get 分开写的,按照需要引用吧
org.springframework.web.client.AsyncRestTemplate org.springframework.web.client.RestTemplate HTTP请求工具类,基于以上两个Rest请求工具模板完成封装HTTP请求,包括同步和异步请求的实现。
java发送http请求的工具类,常用工具类,避免每次都要去网上查
java http请求工具类,包含http get post,xml,json,获取ip地址,以及soup请求
java模拟网络请求,get post put delete四种模式,请求头处理参数处理。此文件仅供大家参考,稍作修改即可。