예제 #1
0
  /**
   * GETメソッド
   *
   * @param url 接続するURL
   * @param requestParams リクエストパラメータ
   * @return HTTPのレスポンス情報
   * @throws LDRException HTTP接続時に例外が発生した場合
   */
  protected LDRHttpResponse doGet(String url, Map<String, String> requestParams)
      throws LDRException {

    HttpGet get = null;

    try {
      StringBuilder builder = new StringBuilder(url);
      builder.append("?");
      for (Map.Entry<String, String> entry : requestParams.entrySet()) {
        builder.append((String) entry.getKey());
        builder.append("=");
        builder.append((String) entry.getValue());
        builder.append("&");
      }

      String tmpUrl = builder.toString();
      tmpUrl = tmpUrl.substring(0, tmpUrl.length() - 1);

      get = new HttpGet(tmpUrl);

      HttpResponse response = httpClient.execute(get);
      LDRHttpResponse ldrResponse = new LDRHttpResponse();
      ldrResponse.setStatusCode(response.getStatusLine().getStatusCode());
      ldrResponse.setHeader(response.getAllHeaders());
      ldrResponse.setBody(EntityUtils.toString(response.getEntity()));
      ldrResponse.setCookieHeader(response.getHeaders("Set-Cookie"));

      return ldrResponse;

    } catch (UnsupportedEncodingException e) {
      throw new LDRException(e);
    } catch (ClientProtocolException e) {
      throw new LDRException(e);
    } catch (IOException e) {
      throw new LDRException(e);
    } finally {
      if (get != null) {
        get.abort();
      }
    }
  }
예제 #2
0
  /**
   * POSTメソッド
   *
   * @param url 接続するURL
   * @param requestParams リクエストパラメータ
   * @return HTTPのレスポンス情報
   * @throws LDRException HTTP接続時に例外が発生した場合
   */
  protected LDRHttpResponse doPost(String url, Map<String, String> requestParams)
      throws LDRException {

    HttpPost post = null;

    try {
      post = new HttpPost(url);

      List<NameValuePair> params = new ArrayList<NameValuePair>();
      for (Map.Entry<String, String> entry : requestParams.entrySet()) {
        params.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue()));
      }

      post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
      HttpResponse response = httpClient.execute(post);

      LDRHttpResponse ldrResponse = new LDRHttpResponse();
      ldrResponse.setStatusCode(response.getStatusLine().getStatusCode());
      ldrResponse.setHeader(response.getAllHeaders());
      ldrResponse.setBody(EntityUtils.toString(response.getEntity()));
      ldrResponse.setCookieHeader(response.getHeaders("Set-Cookie"));

      return ldrResponse;

    } catch (UnsupportedEncodingException e) {
      throw new LDRException(e);
    } catch (ClientProtocolException e) {
      throw new LDRException(e);
    } catch (IOException e) {
      throw new LDRException(e);
    } finally {
      if (post != null) {
        post.abort();
      }
    }
  }