1

In my Java app, I can get name , value pairs from url, but a url with "&" as part of a value is causing problem, it looks like this:

http://localhost:6600/Resume_App?Id=Edit&File_Name=AT&T.txt

I know I can replace the string "AT&T" with "ATT" to avoid it, but I don't want to avoid it, there might be other cases, that I can't predict where this char might occur, so I want to solve it, I'm trying to come up with an intelligent way to tell if "&" is part of a value or not. So for values with "&" char, what's a good way to get name value pairs from it ?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Frank
  • 30,590
  • 58
  • 161
  • 244
  • 3
    You should be escaping the `&` to start with: `http://localhost:6600/Resume_App?Id=Edit&File_Name=AT%26T.txt` – Jon Skeet Aug 26 '15 at 16:35
  • possible duplicate of [escaping ampersand in url](http://stackoverflow.com/questions/16622504/escaping-ampersand-in-url) – lc. Aug 26 '15 at 16:35
  • How did you obtain the badly formed URL? – John Giotta Aug 26 '15 at 16:36
  • Once you have tidied up the URL, you can try using `String.split()`, and then split again on the `=` sign. – Tim Biegeleisen Aug 26 '15 at 16:37
  • need to encode those characters http://www.w3schools.com/tags/ref_urlencode.asp – chrismillah Aug 26 '15 at 16:37
  • Need more info before I can know what to suggest... are you getting the URLs over HTTP (i.e., is your Java application web-enabled and processing incoming URLs), or are you processing a list of stored URLs someplace (in a text file, in a database, etc.) using your app? The context of how you are obtaining the URLs to deal with is crucial to knowing what to do about the problem. – Matt Campbell Aug 26 '15 at 17:46
  • @MattCampbell : thanks for the suggestion. my java web app is processing incoming URLs, trying to get the name and values. – Frank Aug 26 '15 at 23:57

2 Answers2

1

I think you're going at it the wrong way. Instead of trying to figure out if a certain & is part of a value or an argument delimiter, encode the values before sending them to your app (e.g., by using URLEncoder), and decode it on the other side.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

After some trial and error, I finally figured it out :

[1] Use this to encode the URL :

  public static String encode(String s)
  {
    try { return URLEncoder.encode(s,"UTF-8"); }
    catch (UnsupportedEncodingException e)
    {
      Resume_App.Save_To_Log("UTF-8 is not a recognized encoding\n"+Tool_Lib_Simple.Get_Stack_Trace(e));
      throw new RuntimeException(e);
    }
  }

[2] Use "exchange.getRequestURI().getRawQuery()" to get the raw query, I was using exchange.getRequestURI().getQuery() eariler, and it decodes the query for me, and made things confusing, what I need is the raw query.

[3] Use the following to get the name value map :

  public LinkedHashMap<String,String> queryToMap(String query)
  {
    LinkedHashMap<String,String> result=new LinkedHashMap();
    for (String param : query.split("&"))
    {
      String pair[]=param.split("=");
      if (pair.length>1) result.put(pair[0],pair[1]);
      else result.put(pair[0],"");
    }
    return result;
  }

[4] Use the following to get decoded name value pairs :

    for (Map.Entry<String,String> entry : params.entrySet())
    {
      paramName=entry.getKey().replace("+"," ");
      try { paramValue=URLDecoder.decode(entry.getValue(),"utf-8"); }
      catch (Exception e) { e.printStackTrace(); }
    }
Frank
  • 30,590
  • 58
  • 161
  • 244
  • I have vote up, nice way to solve it, I have just for fun build also a method and it and works for me too, my solution is further work and based on previous solution I made http://stackoverflow.com/questions/29381446/how-to-convert-url-parameteres-to-json-format/29388163#29388163. – Maytham Fahmi Aug 27 '15 at 15:45