2

How can I access router ip with android?

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.195.1:80");
Log.d("test",post.toString());


try {
    HttpResponse response = client.execute(post);
    Log.d("test",response.toString());
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()));
    String line="";
    while((line=reader.readLine())!=null){
        Log.d("test", line);
    }
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

I am trying with this code but I am getting connection refused error message in log. I think this is due to router authentication. How can I get access to router?

BulletProof47
  • 141
  • 3
  • 11
  • What are u doing? Because 192.168.195.1 is your gateway? Why do you performe a post? In any case you got this message because probably your router use a session cookie with a session id and most routers refuse http post on port 80... you should try with a get. – Ivan Apr 12 '15 at 13:28
  • I am trying to access the router.You mean that I should use HttpGet instead of HttpPost? I tried with port 80 because the router is using port 80 when I try to access using web browser. – BulletProof47 Apr 12 '15 at 14:23
  • Yep but You must open a session before try to post any command with an GET... For example I worked on an Huawei router, first I opened the page of router to get the cookie (using loopj as library because it supports cookies) then I parsed the session ID (in http response) to performe next HTTP CALL. The main problem is that routers are different, you must check how your router works. I can show you my code but different routers need different code – Ivan Apr 12 '15 at 15:35
  • Thanks for the reply.It will be vert helpful if you show me your code.Thanks. – BulletProof47 Apr 13 '15 at 01:54
  • You are probably looking for the default gateway http://stackoverflow.com/a/5391763/2413303 – EpicPandaForce Apr 13 '15 at 08:53

1 Answers1

0

Using loopj I make an HTTPGet to open a session and save the cookie, the response is parsed to get the sessionId (in my case it's required to send SOAP command to router). Passing the istance of AsyncHttpClient to the second method the session still open (but you can define an AsyncHttpClient as object of Class, I don't know how your code is writed) and performe the command

 public void doCommandToRouter(final String channel, final genericResponse result) {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://" + Constants.WIFICHANNEL.VS_DEFAULT_IP, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] response) {
                String session_id = parseSessionId(new String(response));
                applyChanneltoStation(client,session_id,channel,result);
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] response, Throwable throwable) {
                Log.d(TAG, "Fail");
                result.onError(i);
            }
        });
}
private void applyCommandtoRouter(final AsyncHttpClient client, final String session_id, final String bestChannel, final genericResponse result) {

    numTestChange = 0;
    String  xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">... SOAP COMMAND ...</soapenv:Envelope>";

    HttpEntity entity;
    try {
        entity = new StringEntity(xml, "UTF-8");
    } catch (IllegalArgumentException e) {
        Log.d("HTTP", "StringEntity: IllegalArgumentException");
        return;
    } catch (UnsupportedEncodingException e) {
        Log.d("HTTP", "StringEntity: UnsupportedEncodingException");
        return;
    }
    String  contentType = "text/xml";

    client.post( context, Constants.WIFICHANNEL.VS_SERVICE_URL, entity, contentType, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int i, Header[] headers, byte[] bytes) {
            Log.d(TAG, "post ok");
            //Log.d(TAG, "Response: " + new String(bytes));
            result.onSuccess();
        }

        @Override
        public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            Log.d(TAG, "Fail. Error " + i);
        }

    });
}

To parse the response

    private String parseSessionId(String pageText) {
    String pattern = "(dm_cookie=')(.+)(';)";
    Pattern p  = Pattern.compile(pattern);
    Matcher m = p.matcher(pageText);
    String session_id = "";
    while(m.find()) {
        session_id = m.group(2);
        Log.d(TAG, "Found session id: " + session_id);
    }
    return session_id;
}

Please keep in mind that this works only on my router, you must check how your router works because probably this with ctrl+c crtrl+v doesnt' work.

Ivan
  • 978
  • 6
  • 13
  • I used loopj but its executing onFailure() only, but on your case onSuccess() is executing.I check the status code,I am getting 401 error restricted access. What can I do for that? – BulletProof47 Apr 13 '15 at 15:15
  • What method return 401? The router that I used doesn't required username and password, What type of autentication does router use? – Ivan Apr 13 '15 at 15:45
  • I am using TP Link Router and it is using Basic access authentication which is showing popup for username and password. – BulletProof47 Apr 16 '15 at 08:08
  • BulletProof47 Do you get a solution since 2013? I have the same dilema – nnyerges Nov 18 '18 at 01:11