Android_Jerry

生命不息、奋斗不止

HttpURLConnection-Get方法实现

登录验证代码

/**
     * Get方法登录
     *
     * @param username
     * @param password
     * @return
     */
    public static String loginOfGet(String username, String password) {
        try {
            /**
             * URLEncoder.encode(username,"utf-8")   将 username 转换为UTF-8 的格式
             * URLEncoder.encode(password,"utf-8")   将 password 转换为UTF-8 的格式
             */
            String data = "username=" + URLEncoder.encode(username, "utf-8") + "&password=" + URLEncoder.encode(password, "utf-8");
            URL url = new URL("http://10.0.2.2/servlet/LoginServlet?" + data);
            conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(5000);
            int responseCode = conn.getResponseCode();
            Log.i(TAG, "连接代码:" + responseCode);
            if (responseCode == 200) {
                InputStream is = conn.getInputStream();
                String state = getStringFromInputStream(is);
                return state;

            } else {
                Log.i(TAG, "访问失败" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return null;
    }
阅读更多
版权声明:本文为博主原创文章,转载请声明出处! https://blog.csdn.net/ming2316780/article/details/49925853
文章标签: Android get 登录验证
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

HttpURLConnection-Get方法实现

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭