Example usage for org.apache.http.client.methods HttpUriRequest addHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest addHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest addHeader.

Prototype

void addHeader(String str, String str2);

Source Link

Usage

From source file:com.bigdata.rdf.sail.webapp.TestNanoSparqlClient.java

/**
 * Connect to a SPARQL end point (GET or POST query only).
 * /*from w  w w .ja  v  a2  s.  c  om*/
 * @param opts
 *            The connection options.
 * 
 * @return The connection.
 * 
 * @see <a href="https://sourceforge.net/apps/trac/bigdata/ticket/619">
 *      RemoteRepository class should use application/x-www-form-urlencoded
 *      for large POST requests </a>
 */
private HttpResponse doConnect(final ConnectOptions opts) throws Exception {

    /*
     * Generate the fully formed and encoded URL.
     */

    final StringBuilder urlString = new StringBuilder(opts.serviceURL);

    ConnectOptions.addQueryParams(urlString, opts.requestParams);

    final boolean isLongRequestURL = urlString.length() > 1024;

    if (isLongRequestURL && opts.method.equals("POST") && opts.entity == null) {

        /*
         * URL is too long. Reset the URL to just the service endpoint and
         * use application/x-www-form-urlencoded entity instead. Only in
         * cases where there is not already a request entity (SPARQL query
         * and SPARQL update).
         */

        urlString.setLength(0);
        urlString.append(opts.serviceURL);

        opts.entity = ConnectOptions.getFormEntity(opts.requestParams);

    } else if (isLongRequestURL && opts.method.equals("GET") && opts.entity == null) {

        /*
         * Convert automatically to a POST if the request URL is too long.
         * 
         * Note: [opts.entity == null] should always be true for a GET so
         * this bit is a paranoia check.
         */

        opts.method = "POST";

        urlString.setLength(0);
        urlString.append(opts.serviceURL);

        opts.entity = ConnectOptions.getFormEntity(opts.requestParams);

    }

    if (log.isDebugEnabled()) {
        log.debug("*** Request ***");
        log.debug(opts.serviceURL);
        log.debug(opts.method);
        log.debug("query=" + opts.getRequestParam("query"));
        log.debug(urlString.toString());
    }

    HttpUriRequest request = null;
    try {

        request = RemoteRepository.newRequest(urlString.toString(), opts.method);

        if (opts.requestHeaders != null) {

            for (Map.Entry<String, String> e : opts.requestHeaders.entrySet()) {

                request.addHeader(e.getKey(), e.getValue());

                if (log.isDebugEnabled())
                    log.debug(e.getKey() + ": " + e.getValue());

            }

        }

        //            // conn = doConnect(urlString.toString(), opts.method);
        //            final URL url = new URL(urlString.toString());
        //            conn = (HttpURLConnection) url.openConnection();
        //            conn.setRequestMethod(opts.method);
        //            conn.setDoOutput(true);
        //            conn.setDoInput(true);
        //            conn.setUseCaches(false);
        //            conn.setReadTimeout(opts.timeout);
        //            conn.setRequestProperty("Accept", opts.acceptHeader);
        //            if (log.isDebugEnabled())
        //                log.debug("Accept: " + opts.acceptHeader);

        if (opts.entity != null) {

            //                if (opts.data == null)
            //                    throw new AssertionError();

            //                final String contentLength = Integer.toString(opts.data.length);

            //                conn.setRequestProperty("Content-Type", opts.contentType);
            //                conn.setRequestProperty("Content-Length", contentLength);

            //                if (log.isDebugEnabled()) {
            //                    log.debug("Content-Type: " + opts.contentType);
            //                    log.debug("Content-Length: " + contentLength);
            //                }

            //                final ByteArrayEntity entity = new ByteArrayEntity(opts.data);
            //                entity.setContentType(opts.contentType);

            ((HttpEntityEnclosingRequestBase) request).setEntity(opts.entity);

            //                final OutputStream os = conn.getOutputStream();
            //                try {
            //                    os.write(opts.data);
            //                    os.flush();
            //                } finally {
            //                    os.close();
            //                }

        }

        final HttpResponse response = m_httpClient.execute(request);

        return response;

        //            // connect.
        //            conn.connect();
        //
        //            return conn;

    } catch (Throwable t) {
        /*
         * If something goes wrong, then close the http connection.
         * Otherwise, the connection will be closed by the caller.
         */
        try {

            if (request != null)
                request.abort();

            //                // clean up the connection resources
            //                if (conn != null)
            //                    conn.disconnect();

        } catch (Throwable t2) {
            // ignored.
        }
        throw new RuntimeException(opts.serviceURL + " : " + t, t);
    }

}

From source file:com.appbase.androidquery.callback.AbstractAjaxCallback.java

private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers, AjaxStatus status)
        throws ClientProtocolException, IOException {

    if (AGENT != null) {
        hr.addHeader("User-Agent", AGENT);
    }//from   ww w.ja  v  a  2 s.  c  o m

    if (headers != null) {
        for (String name : headers.keySet()) {
            hr.addHeader(name, headers.get(name));
        }

    }

    if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
        hr.addHeader("Accept-Encoding", "gzip");
    }

    String cookie = makeCookie();
    if (cookie != null) {
        hr.addHeader("Cookie", cookie);
    }

    if (ah != null) {
        ah.applyToken(this, hr);
    }

    DefaultHttpClient client = getClient();

    HttpParams hp = hr.getParams();
    if (proxy != null)
        hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    if (timeout > 0) {
        hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
        hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    }

    HttpContext context = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    request = hr;

    if (abort) {
        throw new IOException("Aborted");
    }

    HttpResponse response = null;

    try {
        //response = client.execute(hr, context);
        response = execute(hr, client, context);
    } catch (HttpHostConnectException e) {

        //if proxy is used, automatically retry without proxy
        if (proxy != null) {
            AQUtility.debug("proxy failed, retrying without proxy");
            hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
            //response = client.execute(hr, context);
            response = execute(hr, client, context);
        } else {
            throw e;
        }
    }

    byte[] data = null;

    String redirect = url;

    int code = response.getStatusLine().getStatusCode();
    String message = response.getStatusLine().getReasonPhrase();
    String error = null;

    HttpEntity entity = response.getEntity();

    File file = null;

    if (code < 200 || code >= 300) {

        InputStream is = null;

        try {

            if (entity != null) {

                is = entity.getContent();
                byte[] s = toData(getEncoding(entity), is);

                error = new String(s, "UTF-8");

                AQUtility.debug("error", error);

            }
        } catch (Exception e) {
            AQUtility.debug(e);
        } finally {
            AQUtility.close(is);
        }

    } else {

        HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        redirect = currentHost.toURI() + currentReq.getURI();

        int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));

        OutputStream os = null;
        InputStream is = null;

        try {
            file = getPreFile();

            if (file == null) {
                os = new PredefinedBAOS(size);
            } else {
                file.createNewFile();
                os = new BufferedOutputStream(new FileOutputStream(file));
            }

            is = entity.getContent();
            if ("gzip".equalsIgnoreCase(getEncoding(entity))) {
                is = new GZIPInputStream(is);
            }

            copy(is, os, (int) entity.getContentLength());

            os.flush();

            if (file == null) {
                data = ((PredefinedBAOS) os).toByteArray();
            } else {
                if (!file.exists() || file.length() == 0) {
                    file = null;
                }
            }

        } finally {
            AQUtility.close(is);
            AQUtility.close(os);
        }

    }

    AQUtility.debug("response", code);
    if (data != null) {
        AQUtility.debug(data.length, url);
    }

    status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file)
            .client(client).context(context).headers(response.getAllHeaders());

}

From source file:org.soyatec.windowsazure.management.ServiceManagementRest.java

@Override
public String createLinuxVirtualMachineDeployment(String serviceName, String deploymentName, String label,
        String roleName, String hostName, String username, String newPassword, String OSImageLink,
        String imageName, String roleSize, AsyncResultCallback callback) {
    if (deploymentName == null)
        throw new IllegalArgumentException("Service name is required!");
    if (label == null)
        throw new IllegalArgumentException("Service label is required!");

    //if (isServiceExist(deploymentName))
    //throw new IllegalArgumentException("Service already exist!");
    /*//from w ww . j  a va  2 s  .  c  o  m
          getBaseUrl() + SERVICES_HOSTEDSERVICES
          + ConstChars.Slash + serviceName + DEPLOYMENTS
          + ConstChars.Slash + deploymentName + ConstChars.Slash
          + ROLE_INSTANCES + ConstChars.Slash + roleInstanceName
          + "?comp=reimage")*/

    HttpUriRequest request = HttpUtilities.createServiceHttpRequest(
            URI.create(getBaseUrl() + SERVICES_VIRTUALMACHINE + ConstChars.Slash + serviceName + DEPLOYMENTS),
            HttpMethod.Post);

    request.setHeader(HeaderNames.ApiVersion, XmsVersion.VERSION_2012_03_01);
    request.addHeader(HeaderNames.ContentType, APPLICATION_XML);

    String base64Label = Base64.encode(label.getBytes());
    String body = MessageFormat.format(CREATE_VIRTUALMACHINE, deploymentName, label, roleName, hostName,
            username, newPassword, OSImageLink, imageName, roleSize);

    System.out.println(body);

    ((HttpEntityEnclosingRequest) request).setEntity(new ByteArrayEntity(body.getBytes()));
    return sendAsynchronousRequest(request, callback);

    // body =
    // "<?xml version=\"1.0\"?><CreateDeployment xmlns=\"http://schemas.microsoft.com/windowsazure\"><Name>testdep</Name><PackageUrl>http://soyatecdemo.blob.core.windows.net/manageusage/simpletest</PackageUrl><Label>c2ltcGxldGVzdA==</Label><Configuration>PD94bWwgdmVyc2lvbj0iMS4wIj8+PFNlcnZpY2VDb25maWd1cmF0aW9uIHNlcnZpY2VOYW1lPSJzaW1wbGV0ZXN0IiB4bWxucz0iaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9TZXJ2aWNlSG9zdGluZy8yMDA4LzEwL1NlcnZpY2VDb25maWd1cmF0aW9uIj4gIDxSb2xlIG5hbWU9IldlYlJvbGUiPiAgICA8SW5zdGFuY2VzIGNvdW50PSIxIi8+ICAgIDxDb25maWd1cmF0aW9uU2V0dGluZ3M+ICAgIDwvQ29uZmlndXJhdGlvblNldHRpbmdzPiAgPC9Sb2xlPjwvU2VydmljZUNvbmZpZ3VyYXRpb24+</Configuration></CreateDeployment>";

}

From source file:org.eclipse.rdf4j.http.client.SPARQLProtocolSession.java

protected HttpUriRequest getQueryMethod(QueryLanguage ql, String query, String baseURI, Dataset dataset,
        boolean includeInferred, int maxQueryTime, Binding... bindings) {
    List<NameValuePair> queryParams = getQueryMethodParameters(ql, query, baseURI, dataset, includeInferred,
            maxQueryTime, bindings);/*from  w  w w.  j a v a2  s  .  co  m*/
    HttpUriRequest method;
    String queryUrlWithParams;
    try {
        URIBuilder urib = new URIBuilder(getQueryURL());
        for (NameValuePair nvp : queryParams)
            urib.addParameter(nvp.getName(), nvp.getValue());
        queryUrlWithParams = urib.toString();
    } catch (URISyntaxException e) {
        throw new AssertionError(e);
    }
    if (shouldUsePost(queryUrlWithParams)) {
        // we just built up a URL for nothing. oh well.
        // It's probably not much overhead against
        // the poor triplestore having to process such as massive query
        HttpPost postMethod = new HttpPost(getQueryURL());
        postMethod.setHeader("Content-Type", Protocol.FORM_MIME_TYPE + "; charset=utf-8");
        postMethod.setEntity(new UrlEncodedFormEntity(queryParams, UTF8));
        method = postMethod;
    } else {
        method = new HttpGet(queryUrlWithParams);
    }
    // functionality to provide custom http headers as required by the
    // applications
    for (Map.Entry<String, String> additionalHeader : additionalHttpHeaders.entrySet()) {
        method.addHeader(additionalHeader.getKey(), additionalHeader.getValue());
    }
    return method;
}

From source file:org.soyatec.windowsazure.management.ServiceManagementRest.java

/**
 * The Create Deployment operation uploads a new service package and creates
 * a new deployment on staging or production.
 * /*from   w w  w .j av a2 s .  c  o m*/
 * </p>
 * 
 * Note that it is possible to call Create Deployment only for a hosted
 * service that has previously been created via the Windows Azure Developer
 * Portal. You cannot upload a new hosted service via the Service Management
 * API.
 * 
 * </p>
 * 
 * The Create Deployment operation is an asynchronous operation. To
 * determine whether the management service has finished processing the
 * request, call Get Operation Status.
 * 
 * @param serviceName
 * @param deploySlotName
 * @param define
 * @return
 */
public String createDeployment(String serviceName, DeploymentSlotType deploySlotName,
        DeploymentConfiguration configuration, AsyncResultCallback callback) {
    if (serviceName == null)
        throw new IllegalArgumentException("Service name is required!");

    if (deploySlotName == null)
        throw new IllegalArgumentException("Deployment slot type is required!");

    if (configuration == null)
        throw new IllegalArgumentException("Deployment configuration is required!");

    configuration.validate();

    HttpUriRequest request = HttpUtilities.createServiceHttpRequest(
            URI.create(getBaseUrl() + SERVICES_HOSTEDSERVICES + ConstChars.Slash + serviceName
                    + DEPLOYMENT_SLOTS + ConstChars.Slash + deploySlotName.getLiteral().toLowerCase()),
            HttpMethod.Post);
    request.addHeader(HeaderNames.ContentType, APPLICATION_XML);
    request.setHeader(HeaderNames.ApiVersion, XmsVersion.VERSION_2010_10_28);

    String content = "";
    if (configuration.getConfigurationFileUrl() != null)
        content = readBase64(configuration.getConfigurationFileUrl());
    else if (configuration.getConfigurationFileStream() != null) {
        try {
            content = readBase64(configuration.getConfigurationFileStream().getBytes());
        } catch (IOException e) {
            throw new StorageException(e);
        }
    }

    String label = configuration.getBase64Label();
    String body = MessageFormat.format(CREATE_DEPLOYMENT_BODY, configuration.getName(),
            configuration.getPackageBlobUrl(), content, label,
            String.valueOf(configuration.isStartDeployment()),
            String.valueOf(configuration.isTreatWarningsAsError())); // configuration.getBase64ConfigurationFile()
    // body =
    // "<?xml version=\"1.0\"?><CreateDeployment xmlns=\"http://schemas.microsoft.com/windowsazure\"><Name>testdep</Name><PackageUrl>http://soyatecdemo.blob.core.windows.net/manageusage/simpletest</PackageUrl><Label>c2ltcGxldGVzdA==</Label><Configuration>PD94bWwgdmVyc2lvbj0iMS4wIj8+PFNlcnZpY2VDb25maWd1cmF0aW9uIHNlcnZpY2VOYW1lPSJzaW1wbGV0ZXN0IiB4bWxucz0iaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9TZXJ2aWNlSG9zdGluZy8yMDA4LzEwL1NlcnZpY2VDb25maWd1cmF0aW9uIj4gIDxSb2xlIG5hbWU9IldlYlJvbGUiPiAgICA8SW5zdGFuY2VzIGNvdW50PSIxIi8+ICAgIDxDb25maWd1cmF0aW9uU2V0dGluZ3M+ICAgIDwvQ29uZmlndXJhdGlvblNldHRpbmdzPiAgPC9Sb2xlPjwvU2VydmljZUNvbmZpZ3VyYXRpb24+</Configuration></CreateDeployment>";

    ((HttpEntityEnclosingRequest) request).setEntity(new ByteArrayEntity(body.getBytes()));
    return sendAsynchronousRequest(request, callback);
}

From source file:com.akop.bach.parser.XboxLiveParser.java

private ContentValues parseSummaryData(XboxLiveAccount account) throws ParserException, IOException {
    long started = System.currentTimeMillis();
    String url = String.format(URL_JSON_PROFILE, mLocale, System.currentTimeMillis());

    HttpUriRequest request = new HttpGet(url);
    request.addHeader("Referer", URL_JSON_PROFILE_REFERER);
    request.addHeader("X-Requested-With", "XMLHttpRequest");

    String page = getResponse(request, null);

    if (App.getConfig().logToConsole())
        started = displayTimeTaken("Profile page fetch", started);

    ContentValues cv = new ContentValues(15);

    String gamertag;// w  w  w.  ja  v  a  2  s . co  m
    JSONObject json = getJSONObject(page);

    try {
        gamertag = json.getString("gamertag");
    } catch (JSONException e) {
        throw new ParserException(mContext, R.string.error_json_parser_error);
    }

    cv.put(Profiles.GAMERTAG, gamertag);
    cv.put(Profiles.ICON_URL, json.optString("gamerpic"));
    cv.put(Profiles.POINTS_BALANCE, 0);
    cv.put(Profiles.IS_GOLD, json.optInt("tier") >= 6);
    cv.put(Profiles.TIER, json.optString("tiertext"));
    cv.put(Profiles.GAMERSCORE, json.optInt("gamerscore"));
    cv.put(Profiles.UNREAD_MESSAGES, json.optInt("messages"));
    cv.put(Profiles.UNREAD_NOTIFICATIONS, json.optInt("notifications"));
    cv.put(Profiles.REP, 0);

    return cv;
}

From source file:org.soyatec.windowsazure.management.ServiceManagementRest.java

@Override
public String startVMRole(String serviceName, String deploymentName, String roleName,
        AsyncResultCallback callback) {//from w  w w.  ja  v  a  2s.c  om

    if (deploymentName == null)
        throw new IllegalArgumentException("Deployment name is required!");
    if (serviceName == null)
        throw new IllegalArgumentException("Service Name is required!");
    if (roleName == null) {
        throw new IllegalArgumentException("RoleName is requred!");
    }

    HttpUriRequest request = HttpUtilities
            .createServiceHttpRequest(
                    URI.create(getBaseUrl() + SERVICES_VIRTUALMACHINE + ConstChars.Slash + serviceName
                            + DEPLOYMENTS + ConstChars.Slash + deploymentName + ConstChars.Slash
                            + ROLE_INSTANCES + ConstChars.Slash + roleName + ConstChars.Slash + OPERATIONS),
                    HttpMethod.Post);

    request.setHeader(HeaderNames.ApiVersion, XmsVersion.VERSION_2012_03_01);
    request.addHeader(HeaderNames.ContentType, APPLICATION_XML);

    String body = START_ROLE_OPERATIONS;

    System.out.println(body);

    ((HttpEntityEnclosingRequest) request).setEntity(new ByteArrayEntity(body.getBytes()));
    return sendAsynchronousRequest(request, callback);

}

From source file:org.soyatec.windowsazure.management.ServiceManagementRest.java

@Override
public void createAffinityGroup(String groupName, String label, String description, String location) {
    HttpUriRequest request = HttpUtilities
            .createServiceHttpRequest(URI.create(getBaseUrl() + SERVICES_AFFINITYGROUPS), HttpMethod.Post);
    if (groupName == null)
        throw new IllegalArgumentException("Group name is required!");

    if (label == null)
        throw new IllegalArgumentException("Label is required!");

    if (location == null)
        throw new IllegalArgumentException("Location is required!");

    if (description == null)
        description = "";

    try {/*  www  .j a v  a2  s. c o  m*/
        request.setHeader(HeaderNames.ApiVersion, XmsVersion.VERSION_2011_02_25);
        request.addHeader(HeaderNames.ContentType, APPLICATION_XML);

        String base64Label = Base64.encode(label.getBytes());
        String body = MessageFormat.format(CREATE_AFFINITY_GROUP, groupName, base64Label, description,
                location);

        ((HttpEntityEnclosingRequest) request).setEntity(new ByteArrayEntity(body.getBytes()));
        HttpWebResponse response = HttpUtilities.getSSLReponse(request, getSslSocketFactory());
        if (response.getStatusCode() == HttpStatus.SC_OK || response.getStatusCode() == HttpStatus.SC_CREATED) {
            return;
        } else {
            HttpUtilities.processUnexpectedStatusCode(response);
        }
    } catch (StorageException we) {
        throw HttpUtilities.translateWebException(we);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.eweb4j.spiderman.plugin.util.PageFetcherImpl.java

/**
 * /*from  ww w . j a  va 2 s  .com*/
 * @date 2013-1-7 ?11:08:54
 * @param toFetchURL
 * @return
 */
public FetchResult request(FetchRequest req) throws Exception {
    FetchResult fetchResult = new FetchResult();
    HttpUriRequest request = null;
    HttpEntity entity = null;
    String toFetchURL = req.getUrl();
    boolean isPost = false;
    try {
        if (Http.Method.GET.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpGet(toFetchURL);
        else if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())) {
            request = new HttpPost(toFetchURL);
            isPost = true;
        } else if (Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpPut(toFetchURL);
        else if (Http.Method.HEAD.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpHead(toFetchURL);
        else if (Http.Method.OPTIONS.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpOptions(toFetchURL);
        else if (Http.Method.DELETE.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpDelete(toFetchURL);
        else
            throw new Exception("Unknown http method name");

        //???,??
        // TODO ?delay?
        synchronized (mutex) {
            //??
            long now = (new Date()).getTime();
            //?Host??
            if (now - lastFetchTime < config.getPolitenessDelay())
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            //????HOST??URL
            lastFetchTime = (new Date()).getTime();
        }

        //GZIP???GZIP?
        request.addHeader("Accept-Encoding", "gzip");
        for (Iterator<Entry<String, String>> it = headers.entrySet().iterator(); it.hasNext();) {
            Entry<String, String> entry = it.next();
            request.addHeader(entry.getKey(), entry.getValue());
        }

        //?
        Header[] headers = request.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = req.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        req.getCookies().putAll(this.cookies);
        fetchResult.setReq(req);

        HttpEntity reqEntity = null;
        if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())
                || Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod())) {
            if (!req.getFiles().isEmpty()) {
                reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                for (Iterator<Entry<String, List<File>>> it = req.getFiles().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<File>> e = it.next();
                    String paramName = e.getKey();
                    for (File file : e.getValue()) {
                        // For File parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new FileBody(file));
                    }
                }

                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        // For usual String parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new StringBody(
                                String.valueOf(paramValue), "text/plain", Charset.forName("UTF-8")));
                    }
                }
            } else {
                List<NameValuePair> params = new ArrayList<NameValuePair>(req.getParams().size());
                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        params.add(new BasicNameValuePair(paramName, String.valueOf(paramValue)));
                    }
                }
                reqEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
            }

            if (isPost)
                ((HttpPost) request).setEntity(reqEntity);
            else
                ((HttpPut) request).setEntity(reqEntity);
        }

        //??
        HttpResponse response = httpClient.execute(request);
        headers = response.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = fetchResult.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        //URL
        fetchResult.setFetchedUrl(toFetchURL);
        String uri = request.getURI().toString();
        if (!uri.equals(toFetchURL))
            if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL))
                fetchResult.setFetchedUrl(uri);

        entity = response.getEntity();
        //???
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode != HttpStatus.SC_NOT_FOUND) {
                Header locationHeader = response.getFirstHeader("Location");
                //301?302?URL??
                if (locationHeader != null && (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || statusCode == HttpStatus.SC_MOVED_TEMPORARILY))
                    fetchResult.setMovedToUrl(
                            URLCanonicalizer.getCanonicalURL(locationHeader.getValue(), toFetchURL));
            }
            //???OKURLstatusCode??
            //???
            if (this.site.getSkipStatusCode() != null && this.site.getSkipStatusCode().trim().length() > 0) {
                String[] scs = this.site.getSkipStatusCode().split(",");
                for (String code : scs) {
                    int c = CommonUtil.toInt(code);
                    //????entity
                    if (statusCode == c) {
                        assemPage(fetchResult, entity);
                        break;
                    }
                }
            }
            fetchResult.setStatusCode(statusCode);
            return fetchResult;
        }

        //??
        if (entity != null) {
            fetchResult.setStatusCode(statusCode);
            assemPage(fetchResult, entity);
            return fetchResult;
        }
    } catch (Throwable e) {
        fetchResult.setFetchedUrl(e.toString());
        fetchResult.setStatusCode(Status.INTERNAL_SERVER_ERROR.ordinal());
        return fetchResult;
    } finally {
        try {
            if (entity == null && request != null)
                request.abort();
        } catch (Exception e) {
            throw e;
        }
    }

    fetchResult.setStatusCode(Status.UNSPECIFIED_ERROR.ordinal());
    return fetchResult;
}