/** * 发生Get请求 * * @param url 请求url * @return * @throws PortalException [参数说明] * @return String [返回类型说明] * @exception throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ public String get(String url) { GetMethod httpMethod = new GetMethod(url); // 设置header信息,传输XML格式的 httpMethod.setRequestHeader(CONTENT_TYPE_NAME, CONTENT_TYPE_VALUE_XML_UTF_8); // 获取响应报文 String response = null; try { // 处理响应结果码 int resultCode = httpClient.executeMethod(httpMethod); if (HttpStatus.SC_OK == resultCode) { byte[] resBody = httpMethod.getResponseBody(); if (null != resBody && resBody.length > 0) { response = new String(resBody, UTF_8); } else { LogUtil.error("Http resultCode=200, but responseBody is empty."); } } else { LogUtil.error("Http resultCode=" + resultCode); } } catch (Exception ex) { LogUtil.error("send http request error.", ex); } finally { if (null != httpMethod) { httpMethod.releaseConnection(); } } return response; }
/** * 发送http请求,并返回响应的xml报文 <功能详细描述> * * @param url http请求url * @param xml 请求xml报文 * @return */ private String send(HttpMethod httpMethod) { // 获取响应报文 String response = null; int resultCode = HttpStatus.SC_OK; try { // 设置header信息,传输XML格式的 httpMethod.setRequestHeader(CONTENT_TYPE_NAME, CONTENT_TYPE_VALUE_XML_UTF_8); // 处理响应结果码 resultCode = httpClient.executeMethod(httpMethod); if (HttpStatus.SC_OK == resultCode) { byte[] resBody = httpMethod.getResponseBody(); if (null != resBody && resBody.length > 0) { response = new String(resBody, UTF_8); } } else { response = resultCode + ""; LogUtil.error("Http response: " + httpMethod.getResponseBodyAsString()); } } catch (Exception ex) { response = ex.toString(); LogUtil.error("send http request error!", ex); } finally { if (null != httpMethod) { httpMethod.releaseConnection(); } } return response; }
/** * 发送http请求,并返回响应的xml报文 <功能详细描述> * * @param url http请求url * @param xml 请求xml报文 * @return */ public String post(String url, String xml) { EntityEnclosingMethod httpMethod = new PostMethod(url); // 发送含xml消息体的对象 try { RequestEntity entity = new StringRequestEntity(xml, TEXT_XML, UTF_8); httpMethod.setRequestEntity(entity); } catch (UnsupportedEncodingException e) { LogUtil.error(e.getMessage(), e); } return send(httpMethod); }
/** * 得到临时目录 * * @return */ public static String getTmpPath() { String tmp = System.getProperty("java.io.tmpdir"); if (tmp == null) { tmp = getWebRoot() + File.separator + "tmp"; File file = new File(tmp); if (!file.exists()) { try { file.mkdir(); } catch (RuntimeException e) { LogUtil.error(e); } } } return tmp; }
public static String getBundledPath(String path) { Bundle bundle = Activator.getDefault().getBundle(); if (bundle == null) { LogUtil.info("getBundlePath(" + path + " bundle is null"); return ""; } try { URL location = FileLocator.toFileURL(bundle.getEntry("/")); File file = new File(location.getPath(), path); LogUtil.info("BundledPath: " + file.getAbsolutePath()); return file.getAbsolutePath(); } catch (Exception ex) { LogUtil.error(ex); return ""; } }
/** * 发送http请求,并返回响应的xml报文 <功能详细描述> * * @param url http请求url * @param xml 请求xml报文 * @return */ public String postJson(String strURL, String json) { try { URL url = new URL(strURL); // 创建连接 HttpURLConnection 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", CONTENT_TYPE_VALUE_TEXT_JSON); // 设置发送数据的格式 connection.connect(); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码 out.append(json); out.flush(); out.close(); // 读取响应 int length = (int) connection.getContentLength(); // 获取长度 InputStream is = connection.getInputStream(); if (length != -1) { byte[] data = new byte[length]; byte[] temp = new byte[512]; int readLen = 0; int destPos = 0; while ((readLen = is.read(temp)) > 0) { System.arraycopy(temp, 0, data, destPos, readLen); destPos += readLen; } String result = new String(data, "UTF-8"); // utf-8编码 return result; } } catch (Exception e) { LogUtil.error(e.getMessage(), e); } return "error"; // 自定义错误信息 }