private HttpMethodParams httpParams(Map<String, String> params) {
   HttpMethodParams methodParams = new HttpMethodParams();
   for (String param : params.keySet()) {
     methodParams.setParameter(param, params.get(param));
   }
   return methodParams;
 }
  /**
   * 发上服务器的文件
   *
   * @param file
   * @return
   */
  private String sendHttpRequest(File file) {
    String string = null;
    PostMethod filePost = new PostMethod(targetURL);
    try {
      Part[] parts = {
        new FilePart("userFile.file", targetFile), new StringPart(paramType, param, "utf-8")
      };
      HttpMethodParams params = filePost.getParams();
      params.setContentCharset("utf-8");
      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(50000);

      Log.v("filePost", filePost.toString());
      Log.v("param", param);
      Log.v("paramType", paramType);
      int status = httpClient.executeMethod(filePost);
      // TODO
      Log.v("status--->", status + "");

      InputStream in = filePost.getResponseBodyAsStream();
      byte[] readStream = readStream(in);
      string = new String(readStream);
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      filePost.releaseConnection();
    }
    return string;
  }
Example #3
0
 /**
  * post
  *
  * @param url
  * @param params
  * @param token
  * @return
  * @throws ApiException
  */
 public static String post(String url, PostParameter[] params, String token) throws IOException {
   PostMethod postMethod = new PostMethod(url);
   for (int i = 0; i < params.length; i++) {
     postMethod.addParameter(params[i].getName(), params[i].getValue());
   }
   HttpMethodParams param = postMethod.getParams();
   param.setContentCharset(DEFAULTCHAESET);
   return httpRequest(postMethod, token);
 }
Example #4
0
 public Response post(String url, PostParameter[] params, Boolean WithTokenHeader, String token)
     throws WeiboException {
   log("Request:");
   log("POST" + url);
   PostMethod postMethod = new PostMethod(url);
   for (int i = 0; i < params.length; i++) {
     postMethod.addParameter(params[i].getName(), params[i].getValue());
   }
   HttpMethodParams param = postMethod.getParams();
   param.setContentCharset("UTF-8");
   return httpRequest(postMethod, WithTokenHeader, token);
 }
  /** Handles the HTTP post. Throws HttpException on failure */
  @SuppressWarnings("deprecation")
  private void doPost(PostMethod method, RequestEntity data, String dest)
      throws IOException, HttpException {

    HttpMethodParams pars = method.getParams();
    pars.setParameter(
        HttpMethodParams.RETRY_HANDLER,
        (Object)
            new HttpMethodRetryHandler() {
              public boolean retryMethod(HttpMethod m, IOException e, int exec) {
                return !(e instanceof java.net.ConnectException)
                    && (exec < MAX_RETRIES_PER_COLLECTOR);
              }
            });
    method.setParams(pars);
    method.setPath(dest);

    // send it across the network
    method.setRequestEntity(data);

    log.info("HTTP post to " + dest + " length = " + data.getContentLength());
    // Send POST request

    client.setTimeout(8000);
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
      log.error(
          "HTTP post response statusCode: "
              + statusCode
              + ", statusLine: "
              + method.getStatusLine());
      // do something aggressive here
      throw new HttpException("got back a failure from server");
    }
    // implicitly "else"
    log.info(
        "got success back from the remote collector; response length "
            + method.getResponseContentLength());

    // FIXME: should parse acks here
    InputStream rstream = null;

    // Get the response body
    byte[] resp_buf = method.getResponseBody();
    rstream = new ByteArrayInputStream(resp_buf);
    BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println("response: " + line);
    }
  }
 public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile)
     throws IOException {
   HttpClient client = new HttpClient();
   NameValuePair[] nameValuePairs = new NameValuePair[4];
   nameValuePairs[0] = new NameValuePair("apikey", apikey);
   nameValuePairs[1] = new NameValuePair("tpl_id", String.valueOf(tpl_id));
   nameValuePairs[2] = new NameValuePair("tpl_value", tpl_value);
   nameValuePairs[3] = new NameValuePair("mobile", mobile);
   PostMethod method = new PostMethod(URI_TPL_SEND_SMS);
   method.setRequestBody(nameValuePairs);
   HttpMethodParams param = method.getParams();
   param.setContentCharset(ENCODING);
   client.executeMethod(method);
   return method.getResponseBodyAsString();
 }
Example #7
0
  /**
   * 주어진 url의 응답 결과를 얻는다.
   *
   * @param url
   * @param params
   * @return HttpResult
   */
  protected HttpResult getHttp(String url, Map<String, String> params) {

    HttpResult result = new HttpResult();

    HttpClient client = getHttpClient();
    HttpMethod method = new GetMethod(url);

    if (params != null) {
      HttpMethodParams param = new HttpMethodParams();

      for (String key : params.keySet()) {
        param.setParameter(key, params.get(key));
      }

      method.setParams(param);
    }

    try {
      int statusCode = client.executeMethod(method);
      result.setStatusCode(statusCode);

      if (statusCode == HttpStatus.SC_OK) {
        // 동적페이지의 경우 last-modified 는 없음.
        InputStream bodyAsStream = method.getResponseBodyAsStream();
        StringBuilder sb = new StringBuilder();
        byte[] b = new byte[1024];
        for (int n; (n = bodyAsStream.read(b)) != -1; ) {
          sb.append(new String(b, 0, n, this.getEnconding()));
        }
        result.setContent(sb.toString());
      }

    } catch (Exception e) {

      logger.error(e.getMessage(), e);
    }

    return result;
  }
  private void configureHttpMethod(
      boolean skipContentCache, CacheData cacheData, long onceTimeOut, HttpMethod httpMethod) {
    if (skipContentCache && null != cacheData) {
      if (null != cacheData.getLastModifiedHeader()
          && Constants.NULL != cacheData.getLastModifiedHeader()) {
        httpMethod.addRequestHeader(Constants.IF_MODIFIED_SINCE, cacheData.getLastModifiedHeader());
      }
      if (null != cacheData.getMd5() && Constants.NULL != cacheData.getMd5()) {
        httpMethod.addRequestHeader(Constants.CONTENT_MD5, cacheData.getMd5());
      }
    }

    httpMethod.addRequestHeader(Constants.ACCEPT_ENCODING, "gzip,deflate");

    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout((int) onceTimeOut);
    httpMethod.setParams(params);
    httpClient
        .getHostConfiguration()
        .setHost(
            diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
            diamondConfigure.getPort());
  }
  // 授权,生成access_token
  // 使用情形:①程序初始化;②每隔一天左右重新授权access_token
  public static void generate() {
    initAccountInfo();
    accessToken.clear();

    logger.info("用户授权中...");
    try {
      // https://api.weibo.com/oauth2/authorize?client_id=750123511&redirect_uri=https://api.weibo.com/oauth2/default.html&response_type=code
      String url = "https://api.weibo.com/oauth2/authorize";
      String redirectUri = "https://api.weibo.com/oauth2/default.html";

      for (int i = 0; i < accountInfo.size(); i++) {
        // 获取应用的信息
        clientId = WeiboConfig.getValue("client_ID");
        clientSecret = WeiboConfig.getValue("client_SERCRET");

        // 构造授权的url参数
        PostMethod postMethod = new PostMethod(url);
        postMethod.addParameter("client_id", clientId);
        postMethod.addParameter("redirect_uri", redirectUri);
        postMethod.addParameter("userId", accountInfo.get(i).getUserId());
        postMethod.addParameter("passwd", accountInfo.get(i).getPasswd());
        postMethod.addParameter("isLoginSina", "0");
        postMethod.addParameter("action", "submit");
        postMethod.addParameter("response_type", "code");
        HttpMethodParams param = postMethod.getParams();
        param.setContentCharset("UTF-8");

        // 伪造头部域信息
        List<Header> headers = new ArrayList<Header>();
        headers.add(
            new Header(
                "Referer",
                "https://api.weibo.com/oauth2/authorize?client_id="
                    + clientId
                    + "&redirect_uri="
                    + redirectUri
                    + "&from=sina&response_type=code"));
        headers.add(new Header("Host", "api.weibo.com"));
        headers.add(
            new Header(
                "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"));

        // 发送HTTP请求
        HttpClient client = new HttpClient();
        client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
        client.executeMethod(postMethod);

        // 获取授权响应
        int status = postMethod.getStatusCode();
        if (status == 302) {
          Header location = postMethod.getResponseHeader("location");
          if (location != null) {
            String retUrl = location.getValue();
            int begin = retUrl.indexOf("code=");
            int end = retUrl.length();
            String code = retUrl.substring(begin + 5, end);
            if (code != null) {
              Oauth oauth = new Oauth();
              String token = oauth.getAccessTokenByCode(code).getAccessToken();
              accessToken.add(token);
              logger.info("第" + (i + 1) + "个access_token:" + token);
            }
          }
        } else {
          logger.error("第" + (i + 1) + "个用户授权失败了!");
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      logger.error("授权发生异常!");
    }
  }
  Set<String> checkUpdateDataIds(long timeout) {
    if (!isRun) {
      throw new RuntimeException("DiamondSubscriber is not running. checkUpdateDataIds return.");
    }
    if (MockServer.isTestMode()) {
      return testData();
    }
    long waitTime = 0;

    String probeUpdateString = getProbeUpdateString();
    if (StringUtils.isBlank(probeUpdateString)) {
      return null;
    }

    while (0 == timeout || timeout > waitTime) {
      long onceTimeOut = getOnceTimeOut(waitTime, timeout);
      waitTime += onceTimeOut;

      PostMethod postMethod = new PostMethod(Constants.HTTP_URI_FILE);

      postMethod.addParameter(Constants.PROBE_MODIFY_REQUEST, probeUpdateString);

      HttpMethodParams params = new HttpMethodParams();
      params.setSoTimeout((int) onceTimeOut);
      postMethod.setParams(params);

      try {
        httpClient
            .getHostConfiguration()
            .setHost(
                diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
                this.diamondConfigure.getPort());

        int httpStatus = httpClient.executeMethod(postMethod);

        switch (httpStatus) {
          case SC_OK:
            {
              Set<String> result = getUpdateDataIds(postMethod);
              return result;
            }

          case SC_SERVICE_UNAVAILABLE:
            {
              rotateToNextDomain();
            }
            break;

          default:
            {
              log.warn("checkUpdateDataIds HTTP State: " + httpStatus);
              rotateToNextDomain();
            }
        }
      } catch (HttpException e) {
        log.error("checkUpdateDataIds HttpException", e);
        rotateToNextDomain();
      } catch (IOException e) {
        log.error("checkUpdateDataIds IOException", e);
        rotateToNextDomain();
      } catch (Exception e) {
        log.error("checkUpdateDataIds Unknown Exception", e);
        rotateToNextDomain();
      } finally {
        postMethod.releaseConnection();
      }
    }
    throw new RuntimeException(
        "checkUpdateDataIds timeout "
            + diamondConfigure.getDomainNameList().get(this.domainNamePos.get())
            + ", timeout="
            + timeout);
  }
Example #11
0
  public void process(Exchange exchange) throws Exception {
    // if we bridge endpoint then we need to skip matching headers with the HTTP_QUERY to avoid
    // sending
    // duplicated headers to the receiver, so use this skipRequestHeaders as the list of headers to
    // skip
    Map<String, Object> skipRequestHeaders = null;

    if (getEndpoint().isBridgeEndpoint()) {
      exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
      String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
      if (queryString != null) {
        skipRequestHeaders = URISupport.parseQuery(queryString);
      }
      // Need to remove the Host key as it should be not used
      exchange.getIn().getHeaders().remove("host");
    }
    HttpMethod method = createMethod(exchange);
    Message in = exchange.getIn();
    String httpProtocolVersion = in.getHeader(Exchange.HTTP_PROTOCOL_VERSION, String.class);
    if (httpProtocolVersion != null) {
      // set the HTTP protocol version
      HttpMethodParams params = method.getParams();
      params.setVersion(HttpVersion.parse(httpProtocolVersion));
    }

    HeaderFilterStrategy strategy = getEndpoint().getHeaderFilterStrategy();

    // propagate headers as HTTP headers
    for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) {
      String key = entry.getKey();
      Object headerValue = in.getHeader(key);

      if (headerValue != null) {
        // use an iterator as there can be multiple values. (must not use a delimiter, and allow
        // empty values)
        final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true);

        // the value to add as request header
        final List<String> values = new ArrayList<String>();

        // if its a multi value then check each value if we can add it and for multi values they
        // should be combined into a single value
        while (it.hasNext()) {
          String value =
              exchange.getContext().getTypeConverter().convertTo(String.class, it.next());

          // we should not add headers for the parameters in the uri if we bridge the endpoint
          // as then we would duplicate headers on both the endpoint uri, and in HTTP headers as
          // well
          if (skipRequestHeaders != null && skipRequestHeaders.containsKey(key)) {
            continue;
          }
          if (value != null
              && strategy != null
              && !strategy.applyFilterToCamelHeaders(key, value, exchange)) {
            values.add(value);
          }
        }

        // add the value(s) as a http request header
        if (values.size() > 0) {
          // use the default toString of a ArrayList to create in the form [xxx, yyy]
          // if multi valued, for a single value, then just output the value as is
          String s = values.size() > 1 ? values.toString() : values.get(0);
          method.addRequestHeader(key, s);
        }
      }
    }

    // lets store the result in the output message.
    try {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Executing http {} method: {}", method.getName(), method.getURI().toString());
      }
      int responseCode = executeMethod(method);
      LOG.debug("Http responseCode: {}", responseCode);

      if (!throwException) {
        // if we do not use failed exception then populate response for all response codes
        populateResponse(exchange, method, in, strategy, responseCode);
      } else {
        if (responseCode >= 100 && responseCode < 300) {
          // only populate response for OK response
          populateResponse(exchange, method, in, strategy, responseCode);
        } else {
          // operation failed so populate exception to throw
          throw populateHttpOperationFailedException(exchange, method, responseCode);
        }
      }
    } finally {
      method.releaseConnection();
    }
  }
  @Override
  public void executeMethod(final HttpMethod method, final ClientRequest cr) {
    final Map<String, Object> props = cr.getProperties();

    method.setDoAuthentication(true);

    final HttpMethodParams methodParams = method.getParams();

    // Set the handle cookies property
    if (!cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES)) {
      methodParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    }

    // Set the interactive and credential provider properties
    if (cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_INTERACTIVE)) {
      CredentialsProvider provider =
          (CredentialsProvider) props.get(ApacheHttpClientConfig.PROPERTY_CREDENTIALS_PROVIDER);
      if (provider == null) {
        provider = DEFAULT_CREDENTIALS_PROVIDER;
      }
      methodParams.setParameter(CredentialsProvider.PROVIDER, provider);
    } else {
      methodParams.setParameter(CredentialsProvider.PROVIDER, null);
    }

    // Set the read timeout
    final Integer readTimeout = (Integer) props.get(ApacheHttpClientConfig.PROPERTY_READ_TIMEOUT);
    if (readTimeout != null) {
      methodParams.setSoTimeout(readTimeout);
    }

    if (method instanceof EntityEnclosingMethod) {
      final EntityEnclosingMethod entMethod = (EntityEnclosingMethod) method;

      if (cr.getEntity() != null) {
        final RequestEntityWriter re = getRequestEntityWriter(cr);
        final Integer chunkedEncodingSize =
            (Integer) props.get(ApacheHttpClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE);
        if (chunkedEncodingSize != null) {
          // There doesn't seems to be a way to set the chunk size.
          entMethod.setContentChunked(true);

          // It is not possible for a MessageBodyWriter to modify
          // the set of headers before writing out any bytes to
          // the OutputStream
          // This makes it impossible to use the multipart
          // writer that modifies the content type to add a boundary
          // parameter
          writeOutBoundHeaders(cr.getHeaders(), method);

          // Do not buffer the request entity when chunked encoding is
          // set
          entMethod.setRequestEntity(
              new RequestEntity() {

                @Override
                public boolean isRepeatable() {
                  return false;
                }

                @Override
                public void writeRequest(OutputStream out) throws IOException {
                  re.writeRequestEntity(out);
                }

                @Override
                public long getContentLength() {
                  return re.getSize();
                }

                @Override
                public String getContentType() {
                  return re.getMediaType().toString();
                }
              });

        } else {
          entMethod.setContentChunked(false);

          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          try {
            re.writeRequestEntity(
                new CommittingOutputStream(baos) {

                  @Override
                  protected void commit() throws IOException {
                    writeOutBoundHeaders(cr.getMetadata(), method);
                  }
                });
          } catch (IOException ex) {
            throw new ClientHandlerException(ex);
          }

          final byte[] content = baos.toByteArray();
          entMethod.setRequestEntity(
              new RequestEntity() {

                @Override
                public boolean isRepeatable() {
                  return true;
                }

                @Override
                public void writeRequest(OutputStream out) throws IOException {
                  out.write(content);
                }

                @Override
                public long getContentLength() {
                  return content.length;
                }

                @Override
                public String getContentType() {
                  return re.getMediaType().toString();
                }
              });
        }
      }
    } else {
      writeOutBoundHeaders(cr.getHeaders(), method);

      // Follow redirects
      method.setFollowRedirects(
          cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_FOLLOW_REDIRECTS));
    }
    try {
      httpClient.executeMethod(
          getHostConfiguration(httpClient, props), method, getHttpState(props));
    } catch (Exception e) {
      method.releaseConnection();
      throw new ClientHandlerException(e);
    }
  }
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    GetMethod httpGet = null;

    try {
      String url = RequestUtil.getParameter(request, PARAM_URL, defaultProxyUrl);
      String host = url.split("/")[2];

      // Get the proxy parameters
      // TODO: Add dependency injection to set proxy config from GeoNetwork settings, using also the
      // credentials configured
      String proxyHost = System.getProperty("http.proxyHost");
      String proxyPort = System.getProperty("http.proxyPort");

      // Get rest of parameters to pass to proxied url
      HttpMethodParams urlParams = new HttpMethodParams();

      Enumeration paramNames = request.getParameterNames();
      while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (!paramName.equalsIgnoreCase(PARAM_URL)) {
          urlParams.setParameter(paramName, request.getParameter(paramName));
        }
      }

      // Checks if allowed host
      if (!isAllowedHost(host)) {
        // throw new ServletException("This proxy does not allow you to access that location.");
        returnExceptionMessage(response, "This proxy does not allow you to access that location.");
        return;
      }

      if (url.startsWith("http://") || url.startsWith("https://")) {
        HttpClient client = new HttpClient();

        // Added support for proxy
        if (proxyHost != null && proxyPort != null) {
          client.getHostConfiguration().setProxy(proxyHost, Integer.valueOf(proxyPort));
        }

        httpGet = new GetMethod(url);
        httpGet.setParams(urlParams);
        client.executeMethod(httpGet);

        if (httpGet.getStatusCode() == HttpStatus.SC_OK) {
          Header contentType = httpGet.getResponseHeader(HEADER_CONTENT_TYPE);
          String[] contentTypesReturned = contentType.getValue().split(";");
          if (!isValidContentType(contentTypesReturned[0])) {
            contentTypesReturned = contentType.getValue().split(" ");
            if (!isValidContentType(contentTypesReturned[0])) {
              throw new ServletException("Status: 415 Unsupported media type");
            }
          }

          // Sets response contentType
          response.setContentType(getResponseContentType(contentTypesReturned));

          String responseBody = httpGet.getResponseBodyAsString().trim();

          PrintWriter out = response.getWriter();
          out.print(responseBody);

          out.flush();
          out.close();

        } else {
          returnExceptionMessage(
              response, "Unexpected failure: " + httpGet.getStatusLine().toString());
        }

        httpGet.releaseConnection();

      } else {
        // throw new ServletException("only HTTP(S) protocol supported");
        returnExceptionMessage(response, "only HTTP(S) protocol supported");
      }
    } catch (Exception e) {
      e.printStackTrace();
      // throw new ServletException("Some unexpected error occurred. Error text was: " +
      // e.getMessage());
      returnExceptionMessage(
          response, "Some unexpected error occurred. Error text was: " + e.getMessage());
    } finally {
      if (httpGet != null) httpGet.releaseConnection();
    }
  }
  protected byte[] URLtoByteArray(
      String location,
      Http.Method method,
      Map<String, String> headers,
      Cookie[] cookies,
      Http.Auth auth,
      Http.Body body,
      List<Http.FilePart> fileParts,
      Map<String, String> parts,
      Http.Response response,
      boolean followRedirects)
      throws IOException {

    byte[] bytes = null;

    HttpMethod httpMethod = null;
    HttpState httpState = null;

    try {
      _cookies.set(null);

      if (location == null) {
        return null;
      } else if (!location.startsWith(Http.HTTP_WITH_SLASH)
          && !location.startsWith(Http.HTTPS_WITH_SLASH)) {

        location = Http.HTTP_WITH_SLASH + location;
      }

      HostConfiguration hostConfiguration = getHostConfiguration(location);

      HttpClient httpClient = getClient(hostConfiguration);

      if (method.equals(Http.Method.POST) || method.equals(Http.Method.PUT)) {

        if (method.equals(Http.Method.POST)) {
          httpMethod = new PostMethod(location);
        } else {
          httpMethod = new PutMethod(location);
        }

        if (body != null) {
          RequestEntity requestEntity =
              new StringRequestEntity(body.getContent(), body.getContentType(), body.getCharset());

          EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod;

          entityEnclosingMethod.setRequestEntity(requestEntity);
        } else if (method.equals(Http.Method.POST)) {
          PostMethod postMethod = (PostMethod) httpMethod;

          processPostMethod(postMethod, fileParts, parts);
        }
      } else if (method.equals(Http.Method.DELETE)) {
        httpMethod = new DeleteMethod(location);
      } else if (method.equals(Http.Method.HEAD)) {
        httpMethod = new HeadMethod(location);
      } else {
        httpMethod = new GetMethod(location);
      }

      if (headers != null) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
          httpMethod.addRequestHeader(header.getKey(), header.getValue());
        }
      }

      if ((method.equals(Http.Method.POST) || method.equals(Http.Method.PUT))
          && ((body != null)
              || ((fileParts != null) && !fileParts.isEmpty())
                  | ((parts != null) && !parts.isEmpty()))) {
      } else if (!hasRequestHeader(httpMethod, HttpHeaders.CONTENT_TYPE)) {
        httpMethod.addRequestHeader(
            HttpHeaders.CONTENT_TYPE, ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED);
      }

      if (!hasRequestHeader(httpMethod, HttpHeaders.USER_AGENT)) {
        httpMethod.addRequestHeader(HttpHeaders.USER_AGENT, _DEFAULT_USER_AGENT);
      }

      httpState = new HttpState();

      if ((cookies != null) && (cookies.length > 0)) {
        org.apache.commons.httpclient.Cookie[] commonsCookies = toCommonsCookies(cookies);

        httpState.addCookies(commonsCookies);

        HttpMethodParams httpMethodParams = httpMethod.getParams();

        httpMethodParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
      }

      if (auth != null) {
        httpMethod.setDoAuthentication(true);

        httpState.setCredentials(
            new AuthScope(auth.getHost(), auth.getPort(), auth.getRealm()),
            new UsernamePasswordCredentials(auth.getUsername(), auth.getPassword()));
      }

      proxifyState(httpState, hostConfiguration);

      httpClient.executeMethod(hostConfiguration, httpMethod, httpState);

      Header locationHeader = httpMethod.getResponseHeader("location");

      if ((locationHeader != null) && !locationHeader.equals(location)) {
        String redirect = locationHeader.getValue();

        if (followRedirects) {
          return URLtoByteArray(
              redirect,
              Http.Method.GET,
              headers,
              cookies,
              auth,
              body,
              fileParts,
              parts,
              response,
              followRedirects);
        } else {
          response.setRedirect(redirect);
        }
      }

      InputStream inputStream = httpMethod.getResponseBodyAsStream();

      if (inputStream != null) {
        Header contentLength = httpMethod.getResponseHeader(HttpHeaders.CONTENT_LENGTH);

        if (contentLength != null) {
          response.setContentLength(GetterUtil.getInteger(contentLength.getValue()));
        }

        Header contentType = httpMethod.getResponseHeader(HttpHeaders.CONTENT_TYPE);

        if (contentType != null) {
          response.setContentType(contentType.getValue());
        }

        bytes = FileUtil.getBytes(inputStream);
      }

      for (Header header : httpMethod.getResponseHeaders()) {
        response.addHeader(header.getName(), header.getValue());
      }

      return bytes;
    } finally {
      try {
        if (httpState != null) {
          _cookies.set(toServletCookies(httpState.getCookies()));
        }
      } catch (Exception e) {
        _log.error(e, e);
      }

      try {
        if (httpMethod != null) {
          httpMethod.releaseConnection();
        }
      } catch (Exception e) {
        _log.error(e, e);
      }
    }
  }