本文实例为大家分享了Java HttpURLConnection使用,供大家参考,具体内容如下
包括使用HttpURLConnection执行get/post请求
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package com.cn.testproject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpConnectionUrlDemo {
public static void main(String[] args) throws Exception {
//get();
post();
}
public static void get() throws Exception {
String path = "http://www.baidu.com";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
byte[] data = toByteArray(inStream);
String result = new String(data, "UTF-8");
System.out.println(result);
}
public static void post() throws Exception {
String encoding = "UTF-8";
//post的form参数(json兼职对)
String params = "[{\\"addTime\\":\\"2011-09-19 14:23:02\\"[],\\"iccid\\":\\"1111\\",\\"id\\":0,\\"imei\\":\\"2222\\",\\"imsi\\":\\"3333\\",\\"phoneType\\":\\"4444\\",\\"remark\\":\\"aaaa\\",\\"tel\\":\\"5555\\"}]";
String path = "http://www.baidu.com";
byte[] data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-javascript; charset=" + encoding);
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode()); // 响应代码 200表示成功
if (conn.getResponseCode() == 200) {
InputStream inStream = conn.getInputStream();
String result = new String(toByteArray(inStream), "UTF-8");
System.out.println(result); // 响应代码 200表示成功
}
}
private static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}
|
GitHub:https://github.com/taz372436
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/taz372436/p/7802233.html
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 40
-
2025-05-29 69
-
2025-06-04 61
-
2025-06-04 62
-
Windows Server 2025 IIS10.0+PHP(FastCGI)+MySQL环境搭建教程
2025-05-26 98
热门评论

