html 读取网络文件,百分求用url类读取网络上html文件的例程!收到立即给分!

|

import java.awt.*;

import java.applet.*;

import java.io.*;

import java.util.*;

import java.net.*;

public class Happy extends Applet

{

private TextArea textArea = new TextArea (25, 70);

public void init ()

{

try

{

URLurl;

URLConnectionurlConn;

DataOutputStreamprintout;

DataInputStreaminput;

// URL of CGI-Bin script.

url = new URL (getCodeBase().toString() + "env.cgi");

// URL connection channel.

urlConn = url.openConnection();

// Let the run-time system (RTS) know that we want input.

urlConn.setDoInput (true);

// Let the RTS know that we want to do output.

urlConn.setDoOutput (true);

// No caching, we want the real thing.

urlConn.setUseCaches (false);

// Specify the content type.

urlConn.setRequestProperty

("Content-Type", "application/x-www-form-urlencoded");

// Send POST output.

printout = new DataOutputStream (urlConn.getOutputStream ());

String content =

"name=" + URLEncoder.encode ("Buford Early") +

"&email=" + URLEncoder.encode ("buford@known-space.com");

printout.writeBytes (content);

printout.flush ();

printout.close ();

// Get response data.

input = new DataInputStream (urlConn.getInputStream ());

String str;

while (null != ((str = input.readLine())))

{

System.out.println (str);

textArea.appendText (str + "n");

}

input.close ();

// Display response.

add ("Center", textArea);

}

catch (MalformedURLException me)

{

System.err.println("MalformedURLException: " + me);

}

catch (IOException ioe)

{

System.err.println("IOException: " + ioe.getMessage());

}

}// End of method init().

}// End of class Happy.

|

import java.net.*; // For Socket stuff

import java.util.*; // For StringTokenizer

import java.io.*; // For PrintStream

//============================================================================

// Takes a URL as input and retrieves the file. Only handles the http

// protocol. Does a direct connection rather than using the URLConnection

// object in order to illustrate the use of sockets.

//

// 8/96 Marty Hall, hall@apl.jhu.edu

// http://www.apl.jhu.edu/~hall/java/

//============================================================================

public class GetURL {

public static int port = 80;

public static void main(String[] args) {

StringTokenizer tok = new StringTokenizer(args[0]);

String protocol = tok.nextToken(":");

String host = tok.nextToken(":/");

String uri;

if (tok.hasMoreTokens())

uri = "/" + tok.nextToken(" ") + "/";

else

uri = "/";

System.out.println("Protocol=" + protocol + ", host=" + host +

", uri=" + uri);

try {

Socket clientSocket = new Socket(host, port);

PrintStream outStream = new PrintStream(clientSocket.getOutputStream());

DataInputStream inStream =

new DataInputStream(clientSocket.getInputStream());

outStream.println("GET " + uri + " HTTP/1.0n");

String line;

while ((line = inStream.readLine()) != null)

System.out.println("> " + line);

} catch(IOException ioe) {

System.out.println("IOException:" + ioe);

} catch(Exception e) {

System.out.println(e.fillInStackTrace());

System.out.println(e.getMessage());

e.printStackTrace();

System.out.println("Error! " + e);

}

}

}

//============================================================================

|

直接用socket就可以了,如:

Socket st = new Socket("http://www.csdn.net/Expert/topic/432/432228.shtm",80);

BufferedInputStream bis = st.getBufferedInputStream();

String s=null;

s = bis.readLine();

while(s!=null){

;写入文件

s = bis.readLine();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值