Example usage for java.net URLEncoder encode

List of usage examples for java.net URLEncoder encode

Introduction

In this page you can find the example usage for java.net URLEncoder encode.

Prototype

public static String encode(String s, Charset charset) 

Source Link

Document

Translates a string into application/x-www-form-urlencoded format using a specific java.nio.charset.Charset Charset .

Usage

From source file:Movies.SearchMovie.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  ww w.j a va2s  .c o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String search = request.getParameter("searchMovie");
    search = URLEncoder.encode(search, "UTF-8");
    URL url = new URL("http://www.omdbapi.com/?i=" + search);

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> map = mapper.readValue(url, Map.class);

    request.setAttribute("movieMap", map);
    request.getRequestDispatcher("movie.jsp").forward(request, response);

}

From source file:org.javiermoreno.torrentscratcher.Runner.java

public Movie enrichMovieWithFilmAffinity(Movie movie) {
    try {//from   w  ww .  j av a  2 s  . c om
        String url = "http://www.filmaffinity.com/es/search.php?stext={title}&stype=all";
        String title = URLEncoder.encode(movie.getTitle(), "UTF8");
        url = url.replace("{title}", title);
        Document doc = Jsoup.connect(url).get();
        if (doc.select("[property=og:title]").size() == 0) {
            // several results found, take the first
            Element firstResult = doc.select(".item-search .mc-title a").first();
            if (firstResult == null) {
                // filmaffinity search engine failed
                log.warn("FilmAffinity 404: " + movie.getTitle());
                return movie;
            }
            url = "http://www.filmaffinity.com" + firstResult.attr("href");
            doc = Jsoup.connect(url).get();
        }
        movie.setFilmAffinityId(doc.select("div.rate-movie-box").attr("data-movie-id"));
        Elements movieInfo = doc.select("dl.movie-info");
        String originalTitle = movieInfo.select("dd").eq(0).text();
        originalTitle = originalTitle.replaceAll("\\([^\\(]*\\)", "").replaceAll("\\[[^\\(]*\\]", "")
                .replaceAll("aka$", "").trim();
        movie.setOriginalTitle(originalTitle);
        movie.setDescription(movieInfo.select("dd").eq(11).text());
    } catch (IOException ex) {
        log.warn(ex.getMessage());
    }
    return movie;
}

From source file:Movies.SearchMovies.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w  w  w. j a  va  2  s.  co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String search = request.getParameter("search");
    search = URLEncoder.encode(search, "UTF-8");
    URL url = new URL("http://www.omdbapi.com/?s=" + search);
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> map = mapper.readValue(url, Map.class);

    List list = (List) map.get("Search");

    request.setAttribute("list", list);

    request.getRequestDispatcher("movies.jsp").forward(request, response);

}

From source file:edu.stolaf.cs.wmrserver.streaming.StreamJob.java

public static void setStreamReducer(JobConf conf, String reduceCommand) {
    conf.setReducerClass(PipeReducer.class);
    try {//from   w  ww .  j  a va  2  s . co  m
        conf.set("stream.reduce.streamprocessor", URLEncoder.encode(reduceCommand, "UTF-8"));
    } catch (java.io.UnsupportedEncodingException ex) {
        throw new RuntimeException("The sky is falling! Java doesn't support UTF-8.");
    }
}

From source file:com.ever365.vfile.protocol.webdav.WebDAVHelper.java

/**
 * Encodes the given string to valid URL format
 * /*from   www .  j a  v  a  2s .c o m*/
 * @param s             the String to convert
 */
public final static String encodeURL(String s) {
    try {
        return replace(URLEncoder.encode(s, "UTF-8"), "+", "%20");
    } catch (UnsupportedEncodingException err) {
        throw new RuntimeException(err);
    }
}

From source file:com.reever.humilheme.web.CookieController.java

public Cookie createCookie(HttpServletRequest request, HttpServletResponse response, String conteudo) {
    String path = StringUtils.isEmpty(request.getContextPath()) ? "/" : request.getContextPath();
    try {/*  ww  w . j a v  a  2 s .  c  o  m*/
        conteudo = URLEncoder.encode(conteudo, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        _logger.error("Erro no encode do cookie", e);
    }
    Cookie cookie = new Cookie(nomeCookie, conteudo);
    cookie.setMaxAge(expiry);
    cookie.setPath(path);
    cookie.setVersion(1);
    response.addCookie(cookie);
    return cookie;
}

From source file:net.bulletin.pdi.xero.step.support.Helpers.java

/**
 * <p>If the value is provided then this method will augment the supplied URL by adding on the
 * query to the URL.</p>/*  ww w .  j  av  a 2s . c o  m*/
 */

public static StringBuilder appendUrlQuery(StringBuilder url, String key, String value) {
    if (null == url || 0 == url.length()) {
        throw new IllegalArgumentException("the supplied url may not be empty.");
    }

    if (StringUtils.isEmpty(key)) {
        throw new IllegalArgumentException("the supplied query key may not be empty.");
    }

    if (StringUtils.isNotBlank(value)) {

        url.append(-1 == url.indexOf("?") ? '?' : '&');
        url.append(key);
        url.append("=");

        try {
            url.append(URLEncoder.encode(value, CharEncoding.UTF_8));
        } catch (UnsupportedEncodingException uee) {
            throw new IllegalStateException("the encoding must be supported; " + CharEncoding.UTF_8);
        }
    }

    return url;
}

From source file:net.jforum.core.tags.URLTag.java

/**
 * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
 *///from  w w w. ja va2 s.  co m
@Override
public void doTag() throws JspException, IOException {

    StringBuilder urlBuilder = new StringBuilder(128).append(this.request().getContextPath());

    if (!encode) {
        urlBuilder.append(this.address);
    } else {
        if (this.address == null) {
            this.address = "";
        }

        String[] addresses = this.address.split("/");

        for (String _address : addresses) {
            if (StringUtils.isNotEmpty(_address)) {
                urlBuilder.append("/").append(URLEncoder.encode(_address, URL_ENCODE));
            }
        }
    }

    this.write(this.response().encodeURL(urlBuilder.toString()));
}

From source file:com.bjwg.back.util.EncodeUtils.java

public static String urlEncode(String part) {
    try {/*from   w  w  w . j  av  a  2  s  . c  om*/
        return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
    } catch (UnsupportedEncodingException e) {
        return "";
    }
}

From source file:com.beginner.core.utils.SmsUtil.java

/**
 * ????/*w  w w . ja  va 2 s. c  om*/
 * @param mobile ?
 * @param code  
 */
public static void sendSms1(String mobile, String code) {
    String account = StringUtils.EMPTY, password = StringUtils.EMPTY, strSMS1 = StringUtils.EMPTY;
    if (null != strSMS1 && !"".equals(strSMS1)) {
        String strS1[] = strSMS1.split(",beginner,");
        if (strS1.length == 2) {
            account = strS1[0];
            password = strS1[1];
        }
    }
    String PostData = "";
    try {
        PostData = "account=" + account + "&password=" + password + "&mobile=" + mobile + "&content="
                + URLEncoder.encode(code, "utf-8");
    } catch (UnsupportedEncodingException e) {
        System.out.println("??");
    }
    //System.out.println(PostData);
    String ret = SMS(PostData, "http://sms.106jiekou.com/utf8/sms.aspx");
    System.out.println(ret);
    /*  
    100         ???
    101         ?
    102         ????
    103         ?
    104         
    105         
    106         ??
    107         Ip??
    108         ?????????
    109         ???
    110         ????
    111         ????
    120         ?
    */

}