• NIO--SocketChannel发送HTTP请求


    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SocketChannel;
    import java.nio.charset.Charset;
     
    /**
     * Created with .
     * Date: 14-5-27
     * Time: 上午11:38
     * To change this template use File | Settings | File Templates.
     */
    public class HttpTest {
     
     
        private static byte[] request = null;
     
        static {
            StringBuffer temp = new StringBuffer();
            temp.append("GET http://localhost:8080/feifei/helloStruts-sayHello.action HTTP/1.1
    ");
            temp.append("Host: 127.0.0.1:8080
    ");
            temp.append("Connection: keep-alive
    ");
            temp.append("Cache-Control: max-age=0
    ");
            temp
                    .append("User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11
    ");
            temp
                    .append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    ");
            temp.append("Accept-Encoding: gzip,deflate,sdch
    ");
            temp.append("Accept-Language: zh-CN,zh;q=0.8
    ");
            temp.append("Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
    ");
            temp.append("
    ");
            request = temp.toString().getBytes();
        }
     
        public static void sendHttpRequest() throws Exception {
            try {
     
                final SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 8080));
                final Charset charset = Charset.forName("GBK");// 创建GBK字符集
                socketChannel.configureBlocking(false);//配置通道使用非阻塞模式
     
                while (!socketChannel.finishConnect()) {
                    Thread.sleep(10);
                }
                //proxySocketChannel.write(charset.encode("GET " + "http://localhost:8080/feifei/helloStruts-sayHello.action HTTP/1.1" + "
    
    "));
                socketChannel.write(ByteBuffer.wrap(request));
     
                int read = 0;
                boolean readed = false;
                ByteBuffer buffer = ByteBuffer.allocate(1024);// 创建1024字节的缓冲
                while ((read = socketChannel.read(buffer)) != -1) {
                    if (read == 0 && readed) {
                        break;
                    } else if (read == 0) {
                        continue;
                    }
     
                    buffer.flip();// flip方法在读缓冲区字节操作之前调用。
                    System.out.println(charset.decode(buffer));
                    // 使用Charset.decode方法将字节转换为字符串
                    buffer.clear();// 清空缓冲
                    readed = true;
                }
                System.out.println("----------------");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public static void main(String[] args) throws Exception {
            sendHttpRequest();
        }
     
     
    }
  • 相关阅读:
    企业架构-发布在线文档【企业架构框架-TOGAF v0.2.pdf】
    以后我的blog部分主题在其他地方写,留意者请继续关注!
    2010年3月blog汇总:企业架构、团队管理
    参加了两天QCon会议,你有什么感觉?
    DDD - 使用聚合(Aggregate)来设计类库
    发布【报表引擎设计.pdf】
    关心你的blog统计数据 给博客增加免费统计服务
    推荐:C2C文档销售与分享社区豆丁
    个人管理 - 后续的个人管理系列文章列表,大家一起来提提建议
    BABOK 需求获取(Elicitation)
  • 原文地址:https://www.cnblogs.com/tiancai/p/9377229.html
Copyright © 2020-2023  润新知