Exemplo n.º 1
0
  public String httpGet(String url, String charset) {
    String httpResult = "";
    AbstractHttpClient httpclient = null;
    try {
      HttpGet httpRequest = new HttpGet(url);

      // 设置 超时机制 毫秒为单位,重连3机制
      HttpParams params = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(params, 30 * 1000);
      HttpConnectionParams.setSoTimeout(params, 45 * 1000);
      HttpConnectionParams.setTcpNoDelay(params, true);
      HttpClientParams.setRedirecting(params, false);

      httpclient = new DefaultHttpClient(params);
      HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(3, true);
      httpclient.setHttpRequestRetryHandler(retryHandler);

      HttpResponse httpResponse = httpclient.execute(httpRequest);

      if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 请求成功
        httpResult = EntityUtils.toString(httpResponse.getEntity(), charset);

        LogUtil.log(
            CT.LOG_CATEGORY_COMMUNICATION,
            "HttpApacheClient  httpGet 请求成功 --> " + httpResult,
            null,
            LogUtilMg.LOG_INFO,
            CT.LOG_PATTERN_COMMUNICATION);

      } else {
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println("HttpApacheClient httpGet返回相应码 --> " + statusCode);
      }
    } catch (Exception e) {
      LogUtil.log(CT.LOG_CATEGORY_ERR, "httpGet请求失败 ", e, LogUtilMg.LOG_ERROR, CT.LOG_PATTERN_ERR);
    } finally {
      if (httpclient != null) httpclient.getConnectionManager().shutdown();
    }
    return httpResult;
  }
Exemplo n.º 2
0
  public JSONObject httpPost(JSONObject requestJson, String url, String charset) {
    JSONObject resultJson = null;
    AbstractHttpClient httpclient = null;
    try {
      HttpPost httpRequest = new HttpPost(url);
      List<NameValuePair> params = new ArrayList<NameValuePair>();

      Iterator<?> it = requestJson.keySet().iterator();
      while (it.hasNext()) {
        Object nextObj = it.next();
        if (nextObj != null) {
          String name = nextObj.toString();
          params.add(new BasicNameValuePair(name, requestJson.getString(name)));
        }
      }

      HttpEntity httpentity = new UrlEncodedFormEntity(params, charset);
      httpRequest.setHeader("accept", "*/*");
      httpRequest.setHeader("connection", "Keep-Alive");
      httpRequest.setEntity(httpentity);

      // 设置超时 毫秒为单位,重连机制
      HttpParams httpParams = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
      HttpConnectionParams.setSoTimeout(httpParams, 45 * 1000);
      HttpConnectionParams.setTcpNoDelay(httpParams, true);
      HttpClientParams.setRedirecting(httpParams, false);

      httpclient = new DefaultHttpClient(httpParams);
      HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(3, true);
      httpclient.setHttpRequestRetryHandler(retryHandler);

      HttpResponse httpResponse = httpclient.execute(httpRequest);

      if (httpResponse.getStatusLine().getStatusCode()
          == HttpStatus.SC_OK) { // HttpStatus.SC_OK表示连接成功
        String httpResult = EntityUtils.toString(httpResponse.getEntity(), charset);

        resultJson = JSONUtils.getJsonFromString(httpResult);
        //				LogUtil.log(CT.LOG_CATEGORY_COMMUNICATION,"HttpApacheClient  httpPost 请求成功 --> " +
        // resultJson,
        //						null, LogUtilMg.LOG_INFO, CT.LOG_PATTERN_COMMUNICATION);
      } else {
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        LogUtil.log(
            CT.LOG_CATEGORY_COMMUNICATION,
            "HttpApacheClient httpPost返回相应码 --> " + statusCode,
            null,
            LogUtilMg.LOG_INFO,
            CT.LOG_PATTERN_COMMUNICATION);
      }
    } catch (Exception e) {
      LogUtil.log(
          CT.LOG_CATEGORY_ERR, "httpPost 请求失败 ", e, LogUtilMg.LOG_ERROR, CT.LOG_PATTERN_ERR);
    } finally {
      if (httpclient != null) {
        httpclient.getConnectionManager().shutdown();
      }
    }
    return resultJson;
  }
Exemplo n.º 3
0
  public String httpPost(String paramContent, String url, String charset) {
    String httpResult = "";
    AbstractHttpClient httpclient = null;
    try {
      HttpPost httpRequest = new HttpPost(url);
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      String[] paramValues = paramContent.split("&");
      for (int i = 0; i < paramValues.length; i++) {
        String[] paramValue = paramValues[i].split("=");
        if (paramValue != null && paramValue.length == 2) {
          params.add(new BasicNameValuePair(paramValue[0], paramValue[1]));
        } else {
          LogUtil.log(
              CT.LOG_CATEGORY_ERR,
              "传递进来的参数解析不合法,原始数据 --> " + paramValues[i],
              null,
              LogUtilMg.LOG_INFO,
              CT.LOG_PATTERN_ERR);
        }
      }

      HttpEntity httpentity = new UrlEncodedFormEntity(params, charset);
      httpRequest.setHeader("accept", "*/*");
      httpRequest.setHeader("connection", "Keep-Alive");
      httpRequest.setEntity(httpentity);

      // 设置超时 毫秒为单位,重连机制
      HttpParams httpParams = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
      HttpConnectionParams.setSoTimeout(httpParams, 45 * 1000);
      HttpConnectionParams.setTcpNoDelay(httpParams, true);
      HttpClientParams.setRedirecting(httpParams, false);

      httpclient = new DefaultHttpClient(httpParams);
      HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(3, true);
      httpclient.setHttpRequestRetryHandler(retryHandler);

      HttpResponse httpResponse = httpclient.execute(httpRequest);

      if (httpResponse.getStatusLine().getStatusCode()
          == HttpStatus.SC_OK) { // HttpStatus.SC_OK表示连接成功
        httpResult = EntityUtils.toString(httpResponse.getEntity(), charset);
        LogUtil.log(
            CT.LOG_CATEGORY_COMMUNICATION,
            "HttpApacheClient  httpPost 请求成功 --> " + httpResult,
            null,
            LogUtilMg.LOG_INFO,
            CT.LOG_PATTERN_COMMUNICATION);
      } else {
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        LogUtil.log(
            CT.LOG_CATEGORY_COMMUNICATION,
            "HttpApacheClient httpPost返回相应码 --> " + statusCode,
            null,
            LogUtilMg.LOG_INFO,
            CT.LOG_PATTERN_COMMUNICATION);
      }
    } catch (Exception e) {
      LogUtil.log(
          CT.LOG_CATEGORY_ERR, "httpPost 请求失败 ", e, LogUtilMg.LOG_ERROR, CT.LOG_PATTERN_ERR);
    } finally {
      if (httpclient != null) httpclient.getConnectionManager().shutdown();
    }
    return httpResult;
  }