Skip to content
Advertisement

output and actual url is different shown in navigation compare to console

currently I’m facing an issue where the URL that are outputted in the console is different from the actual URL display in the nav bar. I look at the value that I had regex, it was correct but when put into IE, it is different. The URL in the nav bar would be only a=test instead of a=test&c=import&f=&uid=user1 which is what is shown in the console. May I know how to solve this? Thanks.

Here is the codes for it.

    UserID user = new UserID();
    
    
    public static void main(String[] args) {
        
        String newUrl = replaceUserID(url);
        System.out.print("cmd.exe /c start iexplore -k " + newUrl);
        
        try{
            Process p = Runtime.getRuntime().exec("cmd.exe /c start iexplore " + newUrl);
            
            try{
                p.waitFor();
            }
            catch( InterruptedException ie ){
                System.out.println("InterruptedException " + ie.getMessage());
            }
            InputStream err = p.getErrorStream();
            int ctr = 0;
            if ( (ctr = err.available()) > 0 ){
                byte[] buf = new byte[ctr];
                System.out.println("Process failed with error:n" + new String(buf, 0, ctr));
                }
            }
            catch(IOException ioe)
            {
                System.out.println("InterruptedException " + ioe.getMessage());
            }
    }
    
    public static String checkUserID(){     
        String username = System.getenv("USERNAME"); //grab user login id for windows
        return username;
    }
    
    public static String replaceUserID(String oldUrl) {
        String params = "http://example.com?a=test&c=import&f=&uid=userid";
        String username = checkUserID();
        
        try {
            Pattern p = Pattern.compile("uid=([^&%]+)");
            Matcher m = p.matcher(params);
            while (m.find()) {
//              System.out.println(m.group(1).toString());
                if(m.group(1).toString() != username) {
                    //replace url paremeters with correct userID.
                    String newUrl = params.replace(m.group(1).toString(), username);
                    return newUrl;
                }
                else {
                    System.out.println("The username is the same");
                    return params;
                }
            } 
        } catch (PatternSyntaxException ex) {
            // error handling
            ex.printStackTrace();
        }
        return oldUrl;
    }

Advertisement

Answer

& has a special meaning to the command line (concat commands, see here: How do I run two commands in one line in Windows CMD?).

You need to escape the url or maybe the entire command. If I remember correctly you need to put double quotes around it: "...iexplore -k "" + newUrl + """

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement