其他分享
首页 > 其他分享> > 网络工具类 post

网络工具类 post

作者:互联网

public static String postData(String url,String phone,String pswd){

try {
    HttpURLConnection connection = null;

    URL url1 = new URL(url);
    connection = (HttpURLConnection) url1.openConnection();
    connection.setReadTimeout(5000);
    connection.setConnectTimeout(5000);
    connection.setRequestMethod("POST");

    String body = "phone=" + URLEncoder.encode(phone) + "&pwd=" + URLEncoder.encode(pswd);
    connection.getOutputStream().write(body.getBytes());

    if (connection.getResponseCode() ==  HttpURLConnection.HTTP_OK){
        InputStream inputStream = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);

        StringBuilder builder = new StringBuilder();
        String str = "";
        while ((str = reader.readLine()) != null){
            builder.append(str);
        }

        connection.disconnect();
        Log.i("aaa", builder.toString());
        return builder.toString();
    }

} catch (Exception e) {
    e.printStackTrace();
}

return null;

}

标签:String,builder,网络工具,phone,connection,new,post,null
来源: https://blog.csdn.net/black_amber/article/details/89305128