当前位置: 技术问答>java相关
百分求用url类读取网络上html文件的例程!收到立即给分!
来源: 互联网 发布时间:2015-03-06
本文导语: | 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 { URL url; ...
|
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
{
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
// 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);
}
}
}
//============================================================================
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();
}
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();
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。