java调用URL

关键类

public String Get_Http(String tUrl) throws IOException{
        URL url = new URL(tUrl);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setConnectTimeout(3000);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        // send data to page
        out.write("username=kevin&password=*********");
        // remember to clean up
        out.flush();
        out.close();
        // this methos can receive msg while the responses is success!
        String sCurrentLine = "";
        String sTotalString = "";
        InputStream l_urlStream;
        l_urlStream = connection.getInputStream();
        BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
        while ((sCurrentLine = l_reader.readLine()) != null) {
            sTotalString += sCurrentLine+"/r/n";  
        }
        return sTotalString;
    }

 

posted @ 2013-11-26 16:57  互联互通社区  阅读(304)  评论(0)    收藏  举报