HttpClient

/**
 * 可以传对象的请求转发,对象已流形式放入HTTP中
 */
public static Object doPost(Map<String,Object> parmMap,String url)
	{
		Object object = null;
		HttpClient hc = new HttpClient();
		
		String fullURL = PropertiesUtils.getValue("app") + url;
		PostMethod pm = new PostMethod(fullURL);
		
		pm.setRequestHeader("Content-Type","application/octet-stream");

		RequestEntity requestEntity = new ByteArrayRequestEntity(SerializationUtils.serialize(parmMap));
		pm.setRequestEntity(requestEntity);
		ObjectInputStream ois = null;
		try {
			hc.executeMethod(pm);
			ois = new ObjectInputStream(pm.getResponseBodyAsStream());
			object = ois.readObject();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return object;
	}

 

/**
	 * @param request
	 * @param response
	 * @param o 流化的对象
	 * 将结果对象写入输出流中
	 */
	protected void processStreamResponse(HttpServletRequest request,HttpServletResponse response,Object o)
	{
		byte[] bt = SerializationUtils.serialize(o);
		OutputStream out = null;
		try {
			out = response.getOutputStream();
			out.write(bt);
			out.close();
		} catch (IOException e) {
			if (null != out) {
				try {
					out.close();
				} catch (IOException e1) {
					
					log.error(e.getMessage(),e);
				}
			}
			log.error(e.getMessage(),e);
		}
	}

 

/**
	 * @param request
	 * @param response
	 * @return 从输入流中读取对象参数
	 */
	protected Map<String, Object> processStreamRequest(HttpServletRequest request,HttpServletResponse response)
	{
		ObjectInputStream oi = null;
		Map<String, Object> map = null;
		try {
			oi = new ObjectInputStream(request.getInputStream());
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		try {
			 map = (HashMap<String, Object>) oi.readObject();
		} catch (IOException e) {			
			log.error(e.getMessage(),e);
		} catch (ClassNotFoundException e) {
			log.error(e.getMessage(),e);
		}
		return map;
		
	}

 

你可能感兴趣的