针对"http://bbs2.greedland.net/index.php"这种地址就可以
而针对http://www.baidu.com/s?ie=gb2312&bs=nio+get%B7%BD%B7%A8&sr=&z=&cl=3&f=8&wd=nio+post+%B7%BD%B7%A8&ct=0
这种带参数的get方法就不行,拿不到任何数据package mingan.MateSearchEngine;import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.net.InetSocketAddress;
import java.io.IOException;public class SearchEngineDownloader
{
private static Charset charset = Charset.defaultCharset();
private static SocketChannel channel; public static void main(String[] args) throws Exception
{
getContent("http://blog.csdn.net/nimeimei/articles/521857.aspx");
} public static void getContent(String requestURL) throws Exception
{
URL url = new URL(requestURL);
int port = url.getPort();
if (port < 0)
port = 80;
String host = url.getHost();
String resource = url.getFile();
 System.out.println(host+"\n"+port+"\n"+resource);
connect(host, port);
sendRequest(resource);
readResponse();
return ; } private static void connect(String host, int port) throws IOException
{// 连接到CSDN
InetSocketAddress socketAddress = new InetSocketAddress(host, port);
channel = SocketChannel.open(socketAddress);
// 使用工厂方法open创建一个channel并将它连接到指定地址上
// 相当与SocketChannel.open().connect(socketAddress);调用
} private static void sendRequest(String path) throws IOException
{
channel.write(charset.encode("GET " + path + "\r\n\r\n"));// 发送GET请求到CSDN的文档中心
// 使用channel.write方法,它需要CharByte类型的参数,使用
// Charset.encode(String)方法转换字符串。
} private static void readResponse() throws IOException
{// 读取应答
ByteBuffer buffer = ByteBuffer.allocate(1024);// 创建1024字节的缓冲
while (channel.read(buffer) != -1)
{
buffer.flip();// flip方法在读缓冲区字节操作之前调用。
System.out.println(charset.decode(buffer));
// 使用Charset.decode方法将字节转换为字符串
buffer.clear();// 清空缓冲
} }
}