0

I have implemented a thread with a server socket in my device that reveal the incoming connection from (for example) a browser. I have found correctly the IP address of the client, but I'm not able to find the url params. Can you please give me help? Please consider this string for example: 192.168.1.110:80/?id=123 or 192.168.1.110/page?id=123 Here there is my class.

public class ServerThread implements Runnable {
    public void run(){
        try{
            if ( SERVERIP != null){
                serverStatus.setText("Listening on IP: " + ip_address_conversion + ":" + SERVERPORT);
                serverSocket = new ServerSocket(SERVERPORT);
                while (true){
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable(){
                        @Override
                        public void run(){
                            serverStatus.setText("Connected");  
                        }
                    });
                    InetAddress ip_client = client.getInetAddress();
                    Log.i("Log", "ip client "+ip_client);
                    //Here i have to find the url params
                }
            }
        } catch (Exception e){
            serverStatus.setText("Error");
        }
    }
}

Thanks

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
Hieicker
  • 595
  • 1
  • 11
  • 29
  • http://stackoverflow.com/questions/6329468/create-http-server-android – nif Jul 02 '13 at 08:33
  • Hi. I already have implemented my server. I have only to find the entire url with params of the incoming connection. – Hieicker Jul 02 '13 at 08:45
  • http://stackoverflow.com/a/11733697/1276374 or regex – Boris Mocialov Jul 02 '13 at 09:00
  • Thanks. The solution seems good, but how can i integrate it with my class? – Hieicker Jul 02 '13 at 09:45
  • You would have to parse the HTTP request header manually. Usually the client will send you a line that looks like `GET /?id=123 HTTP/1.1`. Use Firebug, Wireshark or any other method on your client to look at the request which is sent to the server. Then use e.g. `BufferedReader.readLine()` to read the request line. Finally parse it. – nif Jul 02 '13 at 10:49

1 Answers1

0
public class ServerThread implements Runnable {
    public void run(){
        try{
            if ( SERVERIP != null){
                serverStatus.setText("Listening on IP: " + ip_address_conversion + ":" + SERVERPORT);
                serverSocket = new ServerSocket(SERVERPORT);
                while (true){
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable(){
                        @Override
                        public void run(){
                            serverStatus.setText("Connected");  
                        }
                    });
                    InetAddress ip_client = client.getInetAddress();
                    Log.i("Log", "ip client "+ip_client);
                    //Here i have to find the url params
                    //Find some way to convert your ip_client into string with all parameters together.. or do not use getInetAddress, but rather something easier.
                    LinkedHashMap<String, List<String>> parameters = getQueryParams("192.168.1.110:80/?id=123");  //use String address you get from client object
                }
            }
        } catch (Exception e){
            serverStatus.setText("Error");
        }
    }

    public static LinkedHashMap<String, List<String>> getQueryParams(String url) {
        //You can change to Map or HashMap if order of parameters does not matter for you
        try {
            LinkedHashMap<String, List<String>> params = new LinkedHashMap<String, List<String>>();
            String[] urlParts = url.split("\\?");
            if (urlParts.length > 1) {
                String query = urlParts[1];
                for (String param : query.split("&")) {
                    String[] pair = param.split("=");
                    String key = URLDecoder.decode(pair[0], "UTF-8");
                    String value = "";
                    if (pair.length > 1) {
                        value = URLDecoder.decode(pair[1], "UTF-8");
                    }

                    List<String> values = params.get(key);
                    if (values == null) {
                        values = new ArrayList<String>();
                        params.put(key, values);
                    }
                    values.add(value);
                }
            }

            return params;
        } catch (UnsupportedEncodingException ex) {
            throw new AssertionError(ex);
        }
    }
}

Example:

String query = "192.168.1.110:80/?id=123&something_else=1234&something_else_else=12345";

Output:

  • id | [123]

  • something_else | [1234]

  • something_else_else | [12345]


Source | Reference: https://stackoverflow.com/a/5902142/1276374

Community
  • 1
  • 1
Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55