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\r\n");
temp.append("Host: 127.0.0.1:8080\r\n");
temp.append("Connection: keep-alive\r\n");
temp.append("Cache-Control: max-age=0\r\n");
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\r\n");
temp
.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
temp.append("Accept-Encoding: gzip,deflate,sdch\r\n");
temp.append("Accept-Language: zh-CN,zh;q=0.8\r\n");
temp.append("Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n");
temp.append("\r\n");
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" + "\r\n\r\n"));
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();
} }

NIO--SocketChannel发送HTTP请求的更多相关文章

  1. ReadyAPI/soapUI发送post请求json格式(带有中文字符),后台获取参数为空

    解决:请求编码格式默认为空,在"TestCase"的指定Step的Request Properties中, 改Encoding编码格式为UTF-8. 原文:soapUI发送post ...

  2. Java发送Post请求,参数JSON,接收JSON

    /** * 发送post请求 * @param url 路径 * @param jsonObject 参数(json类型) * @param encoding 编码格式 * @return * @th ...

  3. Java发送Http请求并获取状态码

    通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...

  4. AngularJs的$http发送POST请求,php无法接收Post的数据解决方案

      最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...

  5. Ajax发送POST请求SpringMVC页面跳转失败

    问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...

  6. 使用HttpClient来异步发送POST请求并解析GZIP回应

    .NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...

  7. 在发送ajax请求时加时间戳或者随机数去除js缓存

    在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...

  8. HttpUrlConnection发送url请求(后台springmvc)

    1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...

  9. kattle 发送post请求

    一.简介 kattle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...

  10. 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?

    file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...

随机推荐

  1. Create XML Files Out Of SQL Server With SSIS And FOR XML Syntax

    So you want to spit out some XML from SQL Server into a file, how can you do that? There are a coupl ...

  2. vmware workstation无法打开内核设备问题处理办法

    vmware workstation无法打开内核设备:\\Global\\vmx86 ? 解决办法如下: 开始 - 运行(输入CMD)- 确定或者回车,打开管理员命令窗口: net start vmc ...

  3. ExtJS ComboBox 录入智能提示

    ExtJS ComboBox非常复杂,有很多的属性:其中有的属性是针对某一种特定的方案而设计的,不是所有情况下都有效.我想下拉选择能支持录入,并且录入时能智能提示,弄了半天可以了,但是只能是mode= ...

  4. java学习笔记2--数据类型、数组

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note2.html,转载请注明源地址. 1.数据类型 Java数据类型有: 原始数据类型(Pr ...

  5. 【Python】Django auth 修改密码如何实现?

    使用示例1.创建用户>>> from django.contrib.auth.models import User>>> user = User.objects.c ...

  6. Android编译程序报错:Re-installation failed due to different application signatures.

    如果机子上已经安装非本机编译的android程序,在编译的时候就会报错.方法首选的是删除原程序,然后再进行编译. 但是有一部分程序是烧录在系统程序里面的,无法直接删除,这时候可以使用adb shell ...

  7. C#基础视频教程5.2 如何编写简单的超级热键

    我们前面已经理解了如何使用官方的代码实现鼠标键盘的监控,其实还差一点,因为他的代码只能捕捉单个的按键,而其实我们要的是组合键.什么是组合键呢?比如我想定义同时按下WIN+C是去执行屏幕截图.这只要理解 ...

  8. Discuz常见小问题-如何设置为人工审核

    全局-注册与访问控制-人工审核    

  9. PHP快速入门 如何操作MySQL

    1 创建一个新的数据库,注意类型设置为utf8_general_ci 2 在数据库中创建一个新的表,比如叫做tg_user(先从左侧选择刚才创建的数据库) 3 创建第一个字段,自动编号.我们估计网站的 ...

  10. 用thinkphp执行原生sql

    Controller代码: Demo2Controller.class.php <?php namespace Home\Controller; use Think\Controller; us ...