java编写 网页下载程序

1.模拟登录,并记录登录信息。主要是sessionid,对java开发的网站特别有效,在做门户集成项目的时候发现这个cookie简直太重要了。

public  String openSession()
 {
  String urlStr = "";
   OutputStream out = null;
  InputStream in = null;
  try {
   URL url = new URL(urlStr);
   HttpURLConnection con = (HttpURLConnection) url.openConnection();
   con.setRequestMethod("POST");

   // 打印请求头信息
   Map hfs = con.getHeaderFields();
   Set<String> keys = hfs.keySet();
   for (String str : keys) {
    List<String> vs = (List) hfs.get(str);
    System.out.print(str + ":");
    for (String v : vs) {
     System.out.print(v + "\t");
    }
    System.out.println();
   }
   System.out.println("-----------------------");
   String cookieValue = con.getHeaderField("Set-Cookie");
   System.out.println("cookie value:" + cookieValue);
   String sessionId = cookieValue.substring(0,
     cookieValue.indexOf(";"));
   return sessionId;
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  } finally {
   try {
    if (in != null)
     in.close();
    if (out != null)
     out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

2,下载数据核心代码

 

public  void downImgCore(String sessionId,String sUrl,String sFileUrl)
 {

  InputStream in = null;
  try
  {
   String url = sUrl;//
   URL u = new URL(url);
   HttpURLConnection con = (HttpURLConnection) u.openConnection();
   con.setRequestMethod("GET");
   con.setRequestProperty("Cookie", sessionId);
   con.setConnectTimeout(500);
   in = con.getInputStream();

   File img = new File(sFileUrl);
   // 生成图片
   BufferedOutputStream out = new BufferedOutputStream(
     new FileOutputStream(img));
   byte[] buf = new byte[2048];
   int length = in.read(buf);
   while (length != -1) {
    out.write(buf, 0, length);
    length = in.read(buf);
   }
   
   out.close();

  } catch (Exception e)
  {
   e.printStackTrace();
  } finally
  {
   try {
    if (in != null)
     in.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值