有关接口的

xiaoyaozhao6 2015-07-16 11:04:23
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请求方法里面接收到,求详细
...全文
85 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyaozhao6 2015-07-16
  • 打赏
  • 举报
回复
我接收使用servlet接收的

50,338

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧