0

I am trying to use java.net.HttpURLConnection in order to make a request to a URL like this:

https://example.com/app/?#/something=else&someting2=else2

In order to do this, I need to construct a java.net.url, but the constructor strips away the question mark "?":

java.net.URL url = new URL("https://example.com/app/?#/something=else&someting2=else2");
String string = url.toString(); 
// String is https://example.com/app/#/something=else&someting2=else2

And when I create the connection with url.openConnection(), I just get a 404.

I get why it does this. It righfully recognizes everything after and including the hash # as a url fragment, which means that the actual url becomes https://example.com/app/?. And then, it just strips the trailing question mark. But I need the URL to be rendered as is. This is for an Android app.

How can I force HTTPUrlConnection to make the GET request with the question mark in the URL?

Konstantin Schubert
  • 3,242
  • 1
  • 31
  • 46
  • `%3F` instead of the question mark? – Eugene Aug 08 '17 at 21:25
  • or rather `%23` instead of `#`? – Hugues M. Aug 08 '17 at 21:33
  • @HuguesM. But when I url-encode a URL, I don't encode the #'s, no? How will the server know it has to decode the %23 into a # and then treat it as a fragment marker? Since it was encoded, wouldn't that signal to the server that it should NOT treat it as a fragment marker? – Konstantin Schubert Aug 08 '17 at 21:37
  • A fragment identifier is [not sent to the server](https://stackoverflow.com/a/14462350/6730571), so trying to send one does not make sense. So I was assuming it was not really a fragment marker, and that you wanted to send an actual hash sign as part of the URL to the server, in which case you'd need to encode it to `%23` – Hugues M. Aug 08 '17 at 21:41
  • Ah, that might the case! – Konstantin Schubert Aug 08 '17 at 21:42
  • Posted a proper answer, as I don't like when I see an unanswered question for which I can help, to then discover it was solved in comments :o) – Hugues M. Aug 09 '17 at 06:57

1 Answers1

2

In a URL, the part after ? is a query string, and the part after # is a fragment identifier, as you already noted.

While the query string is sent to the server, the fragment identifier is not, so trying to send one does not make sense.

2 possibilities:

  • if it is a fragment identifier, you don't need to send it and Android's behavior (normalizing the URL) is correct. Stripping the trailing ? (empty query string) should not be a problem as it makes no sense alone (for both server and client). If you want to preserve the fragment in your client code, what you want is to create a URI, use that to display wherever you want, then convert to URL with toURL() when you need to speak with server.

  • if it is not actually a fragment identifier, it's supposed to be part of the query string, so you need to send a hash sign as part of the URL: in that case, you need to url-encode it to %23, the part after the ? won't be stripped, and the server will know what to do with the encoded %23.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65