双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

有关接口的

xiaoyaozhao6 xiaoyaozhao6 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public InputStream post(String strURL, String params) {
HttpURLConnection connection=null;
InputStream inputStream = null;
try {
URL url = new URL(strURL);// 创建连接
connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8"); // utf-8编码
out.append(params);
out.flush();
out.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (connection != null) {
connection.disconnect();
}
}
//读取返回内容
try {
//读取返回内容
inputStream = connection.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}

这是我发送post请求的方法

// 设置编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 接收json

StringBuffer info = new StringBuffer();
InputStream in = request.getInputStream();
BufferedInputStream buf = new BufferedInputStream(in);
byte[] buffer = new byte[1024];

int iRead;
while ((iRead = buf.read(buffer)) != -1) {
info.append(new String(buffer, 0, iRead, "UTF-8"));

}

这是我的接收方法

这是一个接口,别人访问我的我接收完毕处理完数据后,怎么反馈给他一个json结果,并且让他能在发送post请求方法里面接收到,求详细
...全文
评论 1
收藏 已收藏
分享
返回顶部
xiaoyaozhao6
Java 51,407 成员 86,134 内容