http post请求和响应(以及模拟接收代码)

/**
	 * 向服务器发送post请求
	 * @param httpUrl  请求地址
	 * @param xml   请求xml参数
	 * @return  响应xml参数
	 */
	public static String postRequest(String httpUrl,String xml){
		
		String response = "";
		
		URL url = null;
		try {
			url = new URL(httpUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");// 提交模式
			conn.setRequestProperty("Content-length", String.valueOf(xml.length()));
			conn.setRequestProperty("Content-type", "text/xml");
			conn.setDoOutput(true);
			
			OutputStreamWriter out = new OutputStreamWriter(conn
	                    .getOutputStream());  
			
			out.write(xml);
			out.flush();
			out.close();
			
			int code = conn.getResponseCode();// 从 HTTP 响应消息获取状态码
			
			logger.info("从 HTTP 响应消息获取状态码:"+code);
			
			BufferedReader br = new BufferedReader(new InputStreamReader(conn
                    .getInputStream()));
			String temp = "";
			while((temp = br.readLine())!=null ){
				
				response += temp;
			}
			
		} catch (MalformedURLException e) {
			
			logger.error("执行HTTP post请求时发生异常!", e); 

		} catch (IOException e) {
			logger.error("执行HTTP post请求" + url + "时,发生异常!", e); 

		}
		
		
		return response;
	}

 

这是http post方法请求的封转代码,现在贴上模拟接收请求的响应Action端代码。

	/**
	 * 模拟对方接受请求
	 * @param input
	 * @return
	 */
	@Action("testxml")
	public String responceXml(){
		
		logger.info("********");
		HttpServletRequest request = ServletActionContext.getRequest (); 
		
		String responsexml = "";
		
		HttpServletResponse response = ServletActionContext.getResponse();
		logger.info("responsexml:"+responsexml);
		
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	       String temp = "";
			try {
				while((temp = br.readLine())!=null ){
					
					responsexml += temp;
				}
				
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
		System.out.println(responsexml);
		response.setContentType("text/xml");
		response.setContentLength(responsexml.length());
		
		try {
			response.getOutputStream().write(responsexml.getBytes());
			response.getOutputStream().flush();
			response.getOutputStream().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		return null;
	}

 

   调用测试用例:

   ScreenDeleteVedioInter是调用接口类,

              @Autowired
	ScreenDeleteVedioInter sd;

              @Action("testresquest")
	public String testResquest(){
		
		List<Integer> list = new ArrayList<Integer>();
		
		list.add(23);
		list.add(24);
		list.add(25);
		
		
		List<String> goList =  sd.screenDeleteReguest(list);
		
		for(int i=0;i<goList.size();i++){
			
			System.out.println(goList.get(i));
			
		}
		
		return null;
	}
	

  昨天写了好久,也遇到不少问题,现在发布代码,便于以后参考,哈哈

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值