当前位置: 技术问答>java相关
请教高手:关于URL操作
来源: 互联网 发布时间:2015-01-06
本文导语: import java.net.*; import java.io.*; public class robot{ public static void main(String[] arg){ int character; BufferedInputStream bin; try{ if(arg[0]!=null&&arg[0].length()>0){ URL url = new URL(/tech-qa-java/arg[0]/index.html); bin = (BufferedInputStream)url.getContent();...
import java.net.*;
import java.io.*;
public class robot{
public static void main(String[] arg){
int character;
BufferedInputStream bin;
try{
if(arg[0]!=null&&arg[0].length()>0){
URL url = new URL(/tech-qa-java/arg[0]/index.html);
bin = (BufferedInputStream)url.getContent();
while((character = bin.read())>0)
System.out.print((char)character);
System.out.println();
}
}catch(MalformedURLException ex){
System.err.println("MalformedURLException: "+ex.getMessage());
}catch(IOException ex){
System.err.println("IOException: "+ex.getMessage());
}
}
}
编译通过,但运行时出错:
Exception in thread "main" java.lang.ClassCastException
at robot.main(Compiled Code)
import java.io.*;
public class robot{
public static void main(String[] arg){
int character;
BufferedInputStream bin;
try{
if(arg[0]!=null&&arg[0].length()>0){
URL url = new URL(/tech-qa-java/arg[0]/index.html);
bin = (BufferedInputStream)url.getContent();
while((character = bin.read())>0)
System.out.print((char)character);
System.out.println();
}
}catch(MalformedURLException ex){
System.err.println("MalformedURLException: "+ex.getMessage());
}catch(IOException ex){
System.err.println("IOException: "+ex.getMessage());
}
}
}
编译通过,但运行时出错:
Exception in thread "main" java.lang.ClassCastException
at robot.main(Compiled Code)
|
String theCGI = "http://www.server.com/cgi-bin/aCGI.pl";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(/tech-qa-java/theCGI/index.html);
URLConnection c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream());
String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(/tech-qa-java/theCGI/index.html);
URLConnection c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream());
String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}
|
URL myURL = new URL(/tech-qa-java/strURL/index.html);
URLConnection conn = myURL.openConnection();
InputStream inputs = conn.getInputStream();
URLConnection conn = myURL.openConnection();
InputStream inputs = conn.getInputStream();
|
http://www.csdn.net/develop/read_article.asp?id=7635
|
ender是对的,我写错了。:)