如何使用android访问路由器IP?

时间:2015-04-12 12:09:08

标签: java android httpclient router

如何使用android访问路由器IP?

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();
}

我正在尝试使用此代码,但我在日志中收到连接被拒绝错误消息。我认为这是由于路由器身份验证。我怎样才能访问路由器?

1 个答案:

答案 0 :(得分:0)

使用loopj我创建一个HTTPGet来打开会话并保存cookie,解析响应以获取sessionId(在我的情况下,它需要将SOAP命令发送到路由器)。将AsyncHttpClient的istance传递给会话仍然打开的第二种方法(但你可以将AsyncHttpClient定义为Class的对象,我不知道你的代码是如何编写的)并执行命令

 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);
        }

    });
}

解析回复

    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;
}

请记住,这只适用于我的路由器,你必须检查你的路由器是如何工作的,因为这可能与ctrl + c crtrl + v不起作用。