中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android客戶端訪問網(wǎng)絡(luò)工具類

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.Reader;  
import java.net.HttpURLConnection;  
import java.net.MalformedURLException;  
import java.net.URL;  
  
/** 
 * 連接服務(wù)器 
 *  
 * @author yqq_coder 
 *  
 */  
public class LoginUtils {  
  
    public LoginUtils() {  
        // TODO Auto-generated constructor stub  
    }  
    /** 
     * http://10.1.17.208:8080/LoginService/LoginServlet?userName=Lihua&passWord=123456 
     * http://localhost:8080/?userName=Lihua&passWord=123456 
     * @param ip 服務(wù)器IP 
     * @param userName GET方式傳遞參數(shù)用戶名 
     * @param passWord 密碼 
     * @return  
     */  
    public static String connect(String ip, String userName, String passWord) {  
        String str = "http://" + ip  
                + ":8080/LoginService/LoginServlet?userName="+userName+"&passWord="+passWord;  
        URL url=null;  
        InputStream inputStream = null;  
        HttpURLConnection connection = null;  
        StringBuffer sb = null;// 線程安全  
        try {  
            url = new URL(str);//獲得URL對象  
            try {  
                connection = (HttpURLConnection) url.openConnection();  
                connection.setConnectTimeout(3000);  
                connection.setRequestMethod("GET");//GET方式提交參數(shù)  
                connection.setDoOutput(true);//設(shè)置可以向服務(wù)器讀寫  
                connection.setDoInput(true);  
                //請求成功  
                if (connection.getResponseCode() == 200) {  
                    inputStream = connection.getInputStream();  
                    Reader reader = new InputStreamReader(inputStream, "UTF-8");  
                    //打包成字符流  
                    BufferedReader bufferedReader = new BufferedReader(reader);  
                    String str1 = null;  
                    sb = new StringBuffer();  
                    while ((str1 = bufferedReader.readLine()) != null) {  
                        sb.append(str1);  
                    }  
  
                }  
  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        } catch (MalformedURLException e) {  
  
            e.printStackTrace();  
            //關(guān)閉流很重要  
        } finally {  
            if (inputStream != null) {  
                try {  
                    inputStream.close();  
                    inputStream = null;  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
  
            }  
            if (connection != null) {  
                connection.disconnect();  
                connection = null;  
            }  
  
        }  
        if (sb != null) {  
            return new String(sb);  
        }  
  
        return "服務(wù)器異常!";  
  
    }  
  
}  

標(biāo)簽: 安全 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇: android AES加密工具類

下一篇:php將數(shù)據(jù)庫導(dǎo)出成excel的方法