用java / android

时间:2017-09-27 08:39:35

标签: java android encoding base64

我在服务器上运行了一个应用程序。一旦用户尝试连接到服务器,他就会发送GET请求。根据请求中传递的用户名和密码,服务器对用户进行身份验证。

我能够使用用java编写的客户端代码连接到服务器。我用来生成GET请求的代码是:

private String GETrequest(String nMountpoint, String nUsername, String nPassword){
        String requestmsg = "";
        requestmsg += "GET /" + nMountpoint + " HTTP/1.0\r\n";
        requestmsg += "Host: " + nServer +"\r\n";
        requestmsg += "User-Agent: NTRIP Client v1\r\n";
        requestmsg += "Ntrip-Version: Ntrip/2.0\r\n";
        requestmsg += "Accept: */* \r\n";
        requestmsg += "Connection: close\r\n";
        if (nUsername.length() > 0) {
            requestmsg += "Authorization: Basic " + Base64.getEncoder().encodeToString((nUsername + ":" + nPassword).getBytes(StandardCharsets.UTF_8)) + "\r\n\r\n";
        }

        return requestmsg;
    }

当请求到达服务器并处理它时,我收到下一个输出:

GET /mP HTTP/1.0
Host: host.address.net
User-Agent: NTRIP Client v1
Ntrip-Version: Ntrip/2.0
Accept: */*
Connection: close
Authorization: Basic dGVzdHVzZXI6dGVzdHBhc3M=


checking credentials
dGVzdHVzZXI6dGVzdHBhc3M=
lavrovson:exotech
User: testuser,    Pass: testpass

服务器将消息解码为计划,并且进程继续运行。 但是,当我尝试使用Android应用程序连接到服务器时,服务器端的进程卡住了。 我用来在android上生成GET请求的代码是:

private String GETrequest(String nMountpoint, String nUsername, String nPassword){
        String requestmsg = "";
        requestmsg += "GET /" + nMountpoint + " HTTP/1.0\r\n";
        requestmsg += "Host: " + nServer +"\r\n";
        requestmsg += "User-Agent: NTRIP Client v1\r\n";
        requestmsg += "Ntrip-Version: Ntrip/2.0\r\n";
        requestmsg += "Accept: */* \r\n";
        requestmsg += "Connection: close\r\n";
        if (nUsername.length() > 0) {
            String usps = nUsername + ":" + nPassword;
            byte[] bt = null;
            try {
                bt = usps.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {e.printStackTrace();}
            String base64 = Base64.encodeToString(bt, Base64.DEFAULT);
            requestmsg += "Authorization: Basic " + base64 + "\r\n\r\n";
        }

        return requestmsg;
    }

当请求到达服务器并尝试解码时,我收到下一个输出:

GET /mP HTTP/1.0
Host: host.address.net
User-Agent: NTRIP Client v1
Ntrip-Version: Ntrip/2.0
Accept: */*
Connection: close
Authorization: Basic dGVzdHVzZXI6dGVzdHBhc3M=



checking credentials
dGVzdHVzZXI6dGVzdHBhc3M=

该过程卡在Base64编码字符串的解码上。 当尝试解码服务器上的编码字符串时,我使用下一个代码:

byte[] valueDecoded = Base64.getDecoder().decode(decodedString); 
String UserPass = new String(valueDecoded, StandardCharsets.UTF_8);
String[] UP = UserPass.split(":");

修改 一些额外的服务器代码:

 private void messageParsing(byte[] bt){
        String MSG = new String(bt);
        if (bt.length > 4){
            System.out.println(MSG);
        }

        String tokens[] = MSG.split(" ");
        // GET request parsing
        if (tokens[0].matches("GET") && tokens[1].length()>1){
            if (secure){
                if(conn_logger.isTraceEnabled()){
                    conn_logger.trace("Client at adress " + secureSocket.getRemoteSocketAddress());
                }
            } else {
                if(conn_logger.isTraceEnabled()){
                    conn_logger.trace("Client at adress " + socket.getRemoteSocketAddress());
                }
            }
            System.out.println("checking credentials");
            chekCredentials(tokens);
        }
     }

checkCredentials方法:

private void chekCredentials(String[] tokens){
        String[] encoded = tokens[tokens.length-1].split("\r\n");
        System.out.println(encoded[0]);
        byte[] valueDecoded = Base64.getDecoder().decode(encoded[0]);
        System.out.println(new String(valueDecoded));
        String UserPass = new String(valueDecoded, StandardCharsets.UTF_8);
        String[] UP = UserPass.split(":");
        System.out.println("User: " + UP[0] + ",    Pass: " + UP[1]);

        // Some stuff to do with user/password
          .
          .
          .

}

所以问题是为什么服务器代码无法解码来自Android应用程序的base64编码字符串? 我在android端的编码过程有问题吗? 任何帮助,将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

而不是使用" UTF-8"直接作为字符串参数,在android中从字符串执行getBytes时使用StandardCharsets.UTF_8,或者如果你指向较低的api级别,则使用如下所述。

而不是

bt = usps.getBytes("UTF-8");

使用此

bt = usps.getBytes(Charset.forName("UTF-8"))

答案 1 :(得分:0)

问题解决了。 显然在线: requestmsg += "Authorization: Basic " + base64 + "\r\n\r\n" Android在`\ r \ n \ r \ n \ n"。

之前添加了额外的\n