flink电商实时分析系统 第2节 模拟数据上报代码编写
程序员文章站
2022-06-17 08:54:15
...
上篇:flink电商实时分析系统 第1节 springboot 构建上报服务
1、在子模块DSInfoservice代码编写
(1) 在test文件目录下创建一个DsClienttest类
DsClienttest.java
package com.youfan.test;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class DsClienttest {
public static void main(String[] args) {
String message="test";
String adress="http://127.0.0.1:6097/DsInfoSJService/webInfoSJService";
try {
URL url=new URL(adress);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setAllowUserInteraction(true);
conn.setUseCaches(false); //关闭缓存
conn.setReadTimeout(6*1000); //设置时间
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"); //设置浏览器请求
conn.setRequestProperty("content-type","application/x-javascript");
conn.connect();
OutputStream outputStream = conn.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(outputStream);
out.write(message.getBytes());
out.flush();
String temp="";
InputStream in = conn.getInputStream();
byte[]tempbytes=new byte[1024];
while (in.read(tempbytes,0,1024)!=-1){
temp+=new String(tempbytes);
}
System.out.println(conn.getResponseCode());
System.out.println(temp);
}catch (Exception e){
e.printStackTrace();
}
}
}
(2) 先启动主程序类,后启动DsClienttest 类,控制台打印日志信息:
其中,这两个参数的由来
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"); //设置浏览器请求
conn.setRequestProperty("content-type","application/x-javascript");
如图所示: