发送http的post请求并通过request接收post请求体
来源:互联网 发布:区域分割算法优缺点 编辑:程序博客网 时间:2024/05/18 10:58
1、http发送post请求:
/** * 发送post请求 * @param url 请求url * @param content 请求参数 * @return 请求结果。异常或者没拿到返回结果的情况下,请求结果为"" */public static String doPostQuery(String url, String content, int time) throws IOException {if("yes".equals(flag)) { SSLContext sc = null; try { sc = SSLContext.getInstance("SSL"); sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); } catch (Exception e) { e.printStackTrace(); }URL console = new URL(null,url,new sun.net.www.protocol.https.Handler()); HttpsURLConnection conn = (HttpsURLConnection) console.openConnection(); conn.setSSLSocketFactory(sc.getSocketFactory()); conn.setHostnameVerifier(new TrustAnyHostnameVerifier()); conn.setDoOutput(true); conn.setConnectTimeout(time); conn.setReadTimeout(time); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.connect(); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.write(content.getBytes("UTF-8")); // 刷新、关闭 out.flush(); out.close(); InputStream is = null; ByteArrayOutputStream outStream = null;try { is = conn.getInputStream(); if (is != null) { outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } return new String(outStream.toByteArray()); } } catch (Exception e) { e.printStackTrace(); }finally{ if(outStream!=null) outStream.close(); if(is!=null) is.close(); }} else { // 创建post请求 PostMethod method = new PostMethod(url); setMethodHeader(method); try { RequestEntity requestEntity = new ByteArrayRequestEntity(content.getBytes("UTF-8"), "UTF-8"); method.setRequestEntity(requestEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }// 发出请求 String result = ""; int stateCode = 0; try { stateCode = createHttpClient(time).executeMethod(method); } catch (IOException e) { log.error("Http post request failure, reason could be: {}", e.getMessage()); throw e; } log.info("Http post method, stateCode: {}, url: {}, body: {}", stateCode, url, content); // 请求成功 if (stateCode == HttpStatus.SC_OK) { try { result = method.getResponseBodyAsString(); } catch (IOException e) { e.printStackTrace(); } }// 释放资源 releaseHttpConnection(method); return result; }return null;}2、request接收post体:
public static final byte[] readBytes(InputStream is, int contentLen) { if (contentLen > 0) { int readLen = 0; int readLengthThisTime = 0; byte[] message = new byte[contentLen]; try { while (readLen != contentLen) { readLengthThisTime = is.read(message, readLen, contentLen - readLen); if (readLengthThisTime == -1) {// Should not happen. break; } readLen += readLengthThisTime; } return message; } catch (Exception e) { e.printStackTrace(); } } return new byte[] {}; } @RequestMapping(value = "/test") public void testHttp(HttpServletRequest request) { try{ request.setCharacterEncoding("UTF-8"); int size = request.getContentLength(); System.out.println(size); InputStream is = request.getInputStream(); byte[] reqBodyBytes = readBytes(is, size); String res = new String(reqBodyBytes); //获取了post请求中请求体的内容 System.out.println("请求体:"+res); } catch (Exception e) { } System.out.println("获取了结果"); }
阅读全文
0 0
- 发送http的post请求并通过request接收post请求体
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- python通过get,post方式发送http请求和接收http响应的方法
- python通过get,post方式发送http请求和接收http响应的方法
- python通过get,post方式发送http请求和接收http响应的方法
- java发送post请求并接收返回
- HttpWebResponse发送post请求并接收
- 发送http的post请求
- http 发送post请求
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
- 发送Http请求(POST GET)的方法
- 发送Http请求(POST GET)的方法
- 发送Http请求(POST GET)的方法
- bzoj3192: [JLOI2013]删除物品
- 【Python pip 基础命令】
- 数组
- nodejs中搭建mqtt
- Linux Base64命令
- 发送http的post请求并通过request接收post请求体
- 9-18
- 学习相关
- python的 filter, map, reduce, lambda, list comprehension
- java 命令模式
- java核心技术卷 之复选框
- Git的stash命令
- RabbitMQ系列—RabbitMQ介绍
- 制作ubuntu16.04最小docker镜像