0

I am trying to open a link in default browser. I have used the following code.

try {
                HttpPost httpPost;
          httpPost = new HttpPost(
                    "https://evolven.wlb4.apac.nsroot.net:8441/enlight.server/html/scripts/api/assets.jsp?action=table&all=true&"
                    + "envId=92738683&globalCrit=Host%20where%20name:sdcgcgtiblb20pv%20|"
                    + "%20Certificates%20where%20Until%3E1d%20AND%20Until%3C31d%20&%20EvolvenSessionKey="+id+"&json=true");

        //httpPost.setEntity(new StringEntity(data));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        CloseableHttpResponse response = client.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
            String output;
            while ((output = br.readLine()) != null) {
                responseData = responseData + output;
            }
        } else {
            responseData = "{\"status\": \"ERROR\"}";
        }

I am trying to open a link in default browser. I have used the following code.

String myUrl = "http://www.example.com/engine/myProcessor.jsp?Type=A Type&Name=1100110&Char=!"; 

    try {
        Desktop.getDesktop().browse(new URI(myUrl));
    } catch (IOException err) {
        setTxtOutput("Error: "+err.getMessage());
    } catch (URISyntaxException err) {      
        setTxtOutput("Error: "+err.getMessage());
    } catch (Exception err) {
        setTxtOutput("Error: "+err.getMessage());
    }

I am getting URISyntaxException Illegal character in query at index

I think this is because of characters such as | in my URL.

java.lang.IllegalArgumentException: Illegal character in query at index 171: https://evolven.wlb4.apac.nsroot.net:8441/enlight.server/html/scripts/api/assets.jsp?action=table&all=true&envId=92738683&globalCrit=Host%20where%20name:sdcgcgtiblb20pv%20|%20Certificates%20where%20Until%3E1d%20AND%20Until%3C31d%20&%20EvolvenSessionKey=ebc864ab09b8fc7be406c62731b25435ec3155be&json=true
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:73)
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
GeekCoder
  • 150
  • 1
  • 14
  • 3
    I'm confused. When trying to look for a suitable duplicate, I came across [this post](https://stackoverflow.com/questions/18574154/urisyntaxexception-illegal-character-in-query), which is excactly your second snippet copy pasted, including the same text? The answers are right there. However your exception stacktrace actually corresponds with your first snippet, so what is the second snippet there for? – maloomeister Jul 01 '21 at 10:13
  • Here a special character is present | not space – GeekCoder Jul 01 '21 at 10:16
  • 1
    Does this answer your question? [Illegal character at query](https://stackoverflow.com/questions/19126708/illegal-character-at-query) – maloomeister Jul 01 '21 at 10:16
  • 1
    The `|` character must be encoded as `%7C`. – Andreas Jul 01 '21 at 10:17
  • Generally you should [url encode](https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters) your string – Lino Jul 01 '21 at 10:33
  • %7c worked but url encode gave me Caused by: org.apache.http.ProtocolException: Target host is not specified – GeekCoder Jul 01 '21 at 17:00

0 Answers0