0

I need to pass the encoded url but it needs to avoid special characters as well. So how do I encode it? None of the answers on stackoverflow worked for me. Can any one help? I want to do it in java public String tweet_quote_func(Map attrs, FilterParam param) {

    String url = param.getShorturi();
    String text = attrs.get("display");

    if (url != null && text != null) {

        try {

            text = StringEscapeUtils.unescapeHtml(text); // for double quotes

            String encodedurl = "https://twitter.com/intent/tweet?url="+URLEncoder.encode(url, "UTF-8");
            encodedurl = encodedurl + "&text=" + StringUtil.escapeUrl(text);

            return "<span class=\"tweet_quote\"> <a href=" + encodedurl+ ">" + text.trim() + "<span></span></a></span>";

        } catch (UnsupportedEncodingException e) {
            System.err.println(e);
            return null;
        }
    } else
        return null;
}
user3298846
  • 69
  • 2
  • 3
  • 10
  • 1
    Can you post example of input and expected output (just asking since you claim no standard ways posted on SO works for you)? Also can you include your attempts to solve this problem? – Pshemo Feb 27 '14 at 21:06
  • You do HTTP or JSON or XML encoding, as needed. I don't understand the question; maybe you can clarify what you mean. People send XML over HTTP all the time. Can't have magic chars and be well-formed XML, so it's a common problem. – duffymo Feb 27 '14 at 21:07
  • I'm doing it in java. It's kinda urgent – user3298846 Feb 27 '14 at 21:08
  • @user3298846 All questions on SO are urgent. Try to help us help you. Describe specific problem you are facing (examples would be nice), your attempts to solve it and how did it fail. – Pshemo Feb 27 '14 at 21:10
  • URLEncoder might help you. http://stackoverflow.com/questions/213506/java-net-urlencoder-encodestring-is-deprecated-what-should-i-use-instead – Wes Feb 27 '14 at 21:15
  • for URLEncoder the special characters ".", "-", "*", and "_" remain the same – user3298846 Feb 27 '14 at 21:20

1 Answers1

0

You should use URLEncoder for all URL query argument names and their values. Not StringUtils.escapeURL(), whatever that is.

I don't know what you think is different about 'the special characters ".", "-", "*", and "_"', but URLEncoder is defined to do the right thing.

For the URL paths themselves you should use new URI(null, path, null).toASCIIString().

user207421
  • 305,947
  • 44
  • 307
  • 483