CSDN博客

img purecoffee

通过java.net.URL类抓取某个网页的内容 选择自 cqq 的 Blog

发表于2004/10/18 18:41:00  988人阅读

分类: java 应用

 

/**
 文件名 : OpenUrl.java
 描述 :通过 java.net.URL 类访问一个页面,并且返回结果 

  **/
import java.net.*;
import java.io.*;

public class OpenUrl
{
 public String getContent(String strUrl)
 // 一个public方法,返回字符串,错误则返回"error open url"
 {
  try{
   
   URL url=new URL(strUrl);
   BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
   String s="";
   StringBuffer sb=new StringBuffer("");
   while((s=br.readLine())!=null)
   {     
    sb.append(s+"/r/n");    
   }
   br.close();
   return sb.toString();
  }
  catch(Exception e){
   return "error open url" + strUrl;
   
  }  
 }
 
 public static void main(String args[])
 {
  //具体使用方法
  OpenUrl ou=new OpenUrl();
  System.out.println(ou.getContent("http://127.0.0.1"));
 }
 
}

例子:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=GB2312"
pageEncoding="GB2312"
%>
<%@ include file="../ini/incdb.jsp"%> 
<%@ page import="java.net.*"%>

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="../theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>judge_login_type.jsp</TITLE>
</HEAD>
<BODY>
<%
  

   
         String strUrl= null;
         strUrl = "http://202.107.231.3/authcenter/exist.php?acctype=xxxx&idcard=xxxx";
          //访问验证页面并且取得返回的结果。
         try
         {  
            
             URL url=new URL(strUrl);
             URLConnection conn_1=url.openConnection();
            //别忘了指定页面编码,避免MalformedInputException?
           BufferedReader br=new BufferedReader(new InputStreamReader(conn_1.getInputStream(),"utf-8"));
             String s="";
    String line_1;
      out.println("success0!!");
             while((line_1=br.readLine())!=null)
    {
        out.println(line_1);
    }
    br.close();
             //out.println(sb.toString());
         }
         catch(Exception e)
         {
             out.println("error open url" + strUrl); 
    e.printStackTrace();
         } 

  
  
%>
</BODY>
</HTML>

阅读全文
0 0

相关文章推荐

img
取 消
img