当前位置:首页 > 编程开发

使用Socket通道读取web页面

webgou17年前 (2009-10-10)编程开发223

http://www.99inf.net/SoftwareDev/Java/48705.htm
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class GetWebPageDemo {
  public static void main(String args[]) throws Exception {
    String resource, host, file;
    int slashPos;

    resource = "www.java2s.com/index.htm";
    slashPos = resource.indexOf('/'); // find host/file separator
    if (slashPos < 0) {
      resource = resource + "/";
      slashPos = resource.indexOf('/');
    }
    file = resource.substring(slashPos); // isolate host and file parts
    host = resource.substring(0, slashPos);
    System.out.println("Host to contact: '" + host + "'");
    System.out.println("File to fetch : '" + file + "'");

    SocketChannel channel = null;

    try {
      Charset charset = Charset.forName("ISO-8859-1");
      CharsetDecoder decoder = charset.newDecoder();
      CharsetEncoder encoder = charset.newEncoder();

      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
      CharBuffer charBuffer = CharBuffer.allocate(1024);

      InetSocketAddress socketAddress = new InetSocketAddress(host, 80);
      channel = SocketChannel.open();
      channel.connect(socketAddress);

      String request = "GET " + file + " \r\n\r\n";
      channel.write(encoder.encode(CharBuffer.wrap(request)));

      while ((channel.read(buffer)) != -1) {
        buffer.flip();
        decoder.decode(buffer, charBuffer, false);
        charBuffer.flip();
        System.out.println(charBuffer);
        buffer.clear();
        charBuffer.clear();
      }
    } catch (UnknownHostException e) {
      System.err.println(e);
    } catch (IOException e) {
      System.err.println(e);
    } finally {
      if (channel != null) {
        try {
          channel.close();
        } catch (IOException ignored) {
        }
      }
    }

    System.out.println("\nDone.");
  }
} 

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=40

标签: javasocket
分享给朋友:

“使用Socket通道读取web页面” 的相关文章

编译android sdk遇到问题解决办法

make sdk 出现下面错误: find: `frameworks/base/frameworks/base/docs/html': 没有那个文件或目录 find: `out/target/common/docs/gen': 没有那个文件或目录 find: `frameworks/base/fr…

单位dB~

1、dB    dB是一个表征相对值的值,纯粹的比值,只表示两个量的相对大小关系,没有单位,当考虑甲的功率相比于乙功率大或小多少个dB时,按下面计算公式:10log(甲功率/乙功率),如果采用两者的电压比计算,要用20log(甲电压/乙电压)。  &…

FLAC 编码code节选

FLAC库 编码code节选 charset.h…

2410 Wince Eboot 流程

 2410 Wince Eboot 流程 对于嵌入式系统来说,一般都需要一个bootloader来下载和引导操作系统,常用的bootloader有eboot,uboot以及vivi等,对于windows ce来说最理想的bootloader当然是eboot(我也做了利用uboot下载和引导…

vs 2005 map 文件调试技巧

vs 2005 map 文件调试技巧 …

ResultSet概论

  这篇文章并没有给出如何使用ResultSet的具体例子,只是从ResultSet的功能性上进行了详细的讲述。希望这篇文章对大家理解ResultSet能够有所帮助。下面就是这篇文章的具体内容。        …

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。