1、实例化一个IP地址,InetAddress经常和UDP DatagramSockets 和 Socket,ServerSocket 类一起使用
InetAddress address = InetAddress.getByName("http://www.baidu.com");
2、URL和URLConnection
URL url = new URL("http://www.baidu.com");
URLConnection urlConnection = url.openConnection();
//true表示发送post请求
urlConnection.setDoOutput(true);
//获取输入输出流
InputStream input = urlConnection.getInputStream();
OutputStream output = urlConnection.getOutputStream();
循环发送请求
for (int i = 0; i < 50; i++) {
if (i % 2 == 0) {
final int j = i + 1;
new Thread(new Runnable() {
@Override
public void run() {
try {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
URL url = new URL(
"http://10.1.245.176:8443/labelmap/api/labelmap/test/g2PLableStatusUpdate");
URLConnection conn = url.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print("taskId=taskm" + j
+ "&labelStatus=5&description=过时&token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJsbWFkbWluIiwic2NvcGVzIjpbIuS4pOe6p-S6kuWKqOeuoeeQhuWRmCJdLCJ1c2VySWQiOiI4YTgxNzUyZjZjODZmMjM2MDE2Yzg5ZDk1MzBlNWFmOSIsImlzcyI6Imh0dHA6Ly9hc2lhaW5mby5jb20iLCJpYXQiOjE1NjcxMzQ3MzIsImV4cCI6MTU2NzE2MzUzMn0.CdrCQwy11cgNN4KLcH0nAFoij2AJk8EKtG0Cdlfz2OnXO2FAVHuIpjBryE4G1f1Ontt4D0f12ZTHqRZfmEEfUw");
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.err.println(j + "----" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}