爪哇国新游记之二十九----访问URL获取输入流

代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class HttpReader{
    private String urlStr;

    private String result;

    public HttpReader(String urlStr) throws Exception{
        if(urlStr.startsWith("http://")){
            this.urlStr=urlStr;
        }else{
            this.urlStr="http://"+urlStr;
        }

        URL url = new URL(urlStr);// 创建URL对象
        InputStream is = url.openStream();// 获取url中的输入流
        BufferedReader reader=new BufferedReader(new InputStreamReader(is,"UTF-8"));// 指定编码格式,否则容易乱码

        StringBuilder sb=new StringBuilder();
        while(reader.ready()){
            sb.append(reader.readLine());
        }

        result=sb.toString();

        saveResult();
    }

    private void saveResult(){
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("c://result.html"));
            out.write(result);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getUrlStr() {
        return urlStr;
    }

    public String getResult() {
        return result;
    }

    public static void main(String[] args) throws Exception{
        HttpReader r=new HttpReader("http://www.youdao.com/");
        System.out.println(r.getResult());
    }
}

输出:

;function sendRequest(a){var c=Math.random(),b=new Image;b.src=a+"&_="+c;senderIndex>=50&&(senderIndex=0);senders[senderIndex++]=b}function requestLog(a,c){sendRequest("/requestlog?type="+a+"&"+c)} function recordSearchTime(){var a=(new Date).getTime();document.cookie="sbt="+a+";path=/;domain="+document.domain;return!0}var scope = {currentTimes : "1407294262857"};, l = s.length;        for (; i < l; i++) {            r.push({'c': s[i]});        }        return r;    };    var sug = YD.ui.suggest({        // source:"http://www.youdao.com/tglsuggest2/tglsuggest.s?keyfrom=web.index.suggest&o=" + .ns + "&rn=10&h=17&query="        source:"http://sug.so.360.cn/suggest/word?callback=YD.updateCall&encodein=utf-8&encodeout=utf-8&word="        ,menu: YD.cn("sg-wrap")[0], transData: transData    }, "query");})(getNS());</script></body></html>

文件内容:

;function sendRequest(a){var c=Math.random(),b=new Image;b.src=a+"&_="+c;senderIndex>=50&&(senderIndex=0);senders[senderIndex++]=b}function requestLog(a,c){sendRequest("/requestlog?type="+a+"&"+c)} function recordSearchTime(){var a=(new Date).getTime();document.cookie="sbt="+a+";path=/;domain="+document.domain;return!0}var scope = {currentTimes : "1407294262857"};, l = s.length;        for (; i < l; i++) {            r.push({'c': s[i]});        }        return r;    };    var sug = YD.ui.suggest({        // source:"http://www.youdao.com/tglsuggest2/tglsuggest.s?keyfrom=web.index.suggest&o=" + .ns + "&rn=10&h=17&query="        source:"http://sug.so.360.cn/suggest/word?callback=YD.updateCall&encodein=utf-8&encodeout=utf-8&word="        ,menu: YD.cn("sg-wrap")[0], transData: transData    }, "query");})(getNS());</script></body></html>
上一篇:Dynamics CRM 2015/2016新特性之三十四:有了插件日志,调试插件so easy!


下一篇:POJSmallest Difference(暴力全排列)