android post/get 请求

public class LoginServer {
/**
*get的方式请求
*@param username 用户名
*@param password 密码
*@return 返回null 登录异常
/ public static String loginByGet(String username,String password){ //get的方式提交就是url拼接的方式 String path = “http://172.16.168.111:1010/login.php?username=”+username+”&password=”+password; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setRequestMethod(“GET”); //获得结果码 int responseCode = connection.getResponseCode(); if(responseCode ==200){ //请求成功 获得返回的流 InputStream is = connection.getInputStream(); return IOSUtil.inputStream2String(is); }else { //请求失败 return null; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /* * post的方式请求
*@param username 用户名
*@param password 密码
*@return 返回null 登录异常
*/
public static String loginByPost(String username,String password){
String path = “http://172.16.168.111:1010/login.php”;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod(“POST”);

        //数据准备
        String data = "username="+username+"&password="+password;
        //至少要设置的两个请求头
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", data.length()+"");

        //post的方式提交实际上是留的方式提交给服务器
        connection.setDoOutput(true);
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(data.getBytes());

        //获得结果码
        int responseCode = connection.getResponseCode();
        if(responseCode ==200){
            //请求成功
            InputStream is = connection.getInputStream();
            return IOSUtil.inputStream2String(is);
        }else {
            //请求失败
            return null;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注