HttpURLConnection

 

public class VerificationThread extends Thread {

    private static final String TAG = "CM-Verification";
    public String WEB = "http://www.cnblogs.com/muhe221/articles/5580117.html";

    @Override
    public void run() {
        try {
            Log.d(TAG, "HTC+++++begin");
            urlConGetWebData();
            Log.d(TAG, "HTC+++++finish");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void urlConGetWebData() throws IOException {
        URL url = new URL(WEB);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            Log.d("TAG", "HttpURLConnection---success--");
            InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
            int i;
            String content = "";
            while ((i = isr.read()) != -1) {
                content = content + (char) i;
            }

            Log.d(TAG, "" + content);
            isr.close();
            httpConn.disconnect();
        } else {
            Log.w("TAG", "HttpURLConnection---fail--");
        }
    }
}

 

posted @ 2016-06-13 12:45  牧 天  阅读(122)  评论(0)    收藏  举报