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. Git分布式开发之生成ssh公钥

    1.在Preferences>Network Connections>SSH2,切换至Key Management面板,点击 2.点击生成Genarate RSA Key,并修Commne ...

  2. Power Desginer系列03【转载】

    在PowerDesigner中设计物理模型1——表和主外键 在PD中建立物理模型由以下几种办法: 直接新建物理模型. 设计好概念模型,然后由概念模型生成物理模型. 设计好逻辑模型,然后由逻辑模型生成物 ...

  3. django 实现上传文件功能

    需求:自己写一个文件上传功能 代码: urls.py from django.conf.urls import url from django.contrib import admin from ap ...

  4. OkHttp 使用案例 文档翻译 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. centos7 selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH

    1.查看安装的chrome浏览器版本 2.查看版本对应的驱动 https://sites.google.com/a/chromium.org/chromedriver/downloads 下载后拷贝到 ...

  6. Javascript-history.go()和history.back()的用法和区别

    简单的说就是:go(-1): 返回上一页,原页面表单中的内容会丢失:back(): 返回上一页,原页表表单中的内容会保留. history.go(-1):后退+刷新 history.back():后退 ...

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

    跟前面一章讲解计算器一样,到最后一小节,我们总是要把代码规整好,让整个程序显得非常简洁,先做个文件夹把我们自定义的类库都放进去   然后我们开始整理Form1里面的代码,为了实现超级热键的功能,我们应 ...

  8. java线程同步问题——由腾讯笔试题引发的风波

    刚刚wm问我了一道线程的问题,因为自己一直是coder界里的渣渣.所以就须要恶补一下. 2016年4月2号题目例如以下. import java.util.logging.Handler; /** * ...

  9. Linux回环接口-----(loop-back/loopback)

    回环接口(loop-back/loopback) Moakap整理 Loopback接口是一个虚拟网络接口,在不同的领域,其含义也大不一样. 1. TCP/IP协议栈中的loopback接口 在TCP ...

  10. error: command 'gcc' failed with exit status 1 while installing eventlet

    Ubuntu安装Python包出错解决办法 : sudo apt-get install python-dev sudo apt-get install libevent-dev