根据URL获取网页TXT源码【Android】

来源:互联网 发布:淘宝网排名怎么靠前 编辑:程序博客网 时间:2024/06/04 00:58
public static String downLoad(String url) {    String response = "";    //第一步:创建HttpClient对象    HttpClient httpClient = new DefaultHttpClient();    //第二步:创建代表请求的对象,参数是访问的服务器地址    HttpGet httpGet = new HttpGet(url);    //第三步:执行请求,获取服务器发还的相应对象    HttpResponse httpResponse = null;    try {        httpResponse = httpClient.execute(httpGet);        //第四步:检查相应的状态是否正常:检查状态码的值是200表示正常        if (httpResponse.getStatusLine().getStatusCode() == 200) {            //第五步:从相应对象当中取出数据,放到entity当中            HttpEntity entity = httpResponse.getEntity();            response = EntityUtils.toString(entity, "utf-8");//将entity当中的数据转换为字符串        } else {            response = "";        }    } catch (IOException e) {        e.printStackTrace();        response = "";    }finally {        httpClient.getConnectionManager().shutdown();    }    return response;}
原创粉丝点击