java httppost raw
最近开发中需要从一个第三方系统中获取数据,使用到了httpclient方法:
httpclient raw请求:
/**
* java发送raw
* @url 请求地址
* @param 请求参数
* @retu 返回响应内容
*/
public static String rawPost(String url,String param) {
//HttpClients.createDefault()等价于 HttpClientBuilder.create().build();
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httpost = new HttpPost(url);
//JSONObject jsonString = JSON.parseObject(param);
//设置header
httpost.setHeader("Content-type", "application/json");
httpost.addHeader("appid", "502");
httpost.addHeader("useame", "menhu");
//组织请求参数
StringEntity stringEntity = new StringEntity(param);
httpost.setEntity(stringEntity);
String content = null;
CloseableHttpResponse httpResponse = null;
try {
//响应信息
httpResponse = closeableHttpClient.execute(httpost);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try { //关闭连接、释放资源
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
retu content;
}
客户端获取请求的参数
注意事项:获取请求参数时使用request.getParameter无法获取参数,需要使用流的方式来获取具体的请求参数:
作者:weixin_39551554
来源链接:https://blog.csdn.net/weixin_39551554/article/details/114902468
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。