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\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请求的更多相关文章
- ReadyAPI/soapUI发送post请求json格式(带有中文字符),后台获取参数为空
解决:请求编码格式默认为空,在"TestCase"的指定Step的Request Properties中, 改Encoding编码格式为UTF-8. 原文:soapUI发送post ...
- Java发送Post请求,参数JSON,接收JSON
/** * 发送post请求 * @param url 路径 * @param jsonObject 参数(json类型) * @param encoding 编码格式 * @return * @th ...
- Java发送Http请求并获取状态码
通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...
- Ajax发送POST请求SpringMVC页面跳转失败
问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...
- 使用HttpClient来异步发送POST请求并解析GZIP回应
.NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...
- 在发送ajax请求时加时间戳或者随机数去除js缓存
在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...
- HttpUrlConnection发送url请求(后台springmvc)
1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...
- kattle 发送post请求
一.简介 kattle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...
- 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?
file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...
随机推荐
- JavaScript的学习要点
概要 了解Javascript历史以及Javascript三个不同组成部分: ECMAScript DOM(文档对象模型) BOM(浏览器对象模型) ECMAScript 目标 掌握Javascrip ...
- 如何移植openwrt系统
Cisco/Linksys在2003年发布了WRT54G这款无线路由器,同年有人发现它的IOS是基于Linux的,然而Linux是基于GPL许可证发布的,按照该许可证Cisco应该把WRT54G的IO ...
- Nicescroll滚动条插件的用法
Nicescroll滚动条插件是一个非常强大的基于jQuery的滚动条插件,不需要增加额外的css,几乎全浏览器兼容.ie6+,实现只需要一段代码,侵入性非常小,样式可完全自定义,支持触摸事件,可在触 ...
- linux 处理键盘 鼠标事件
Linux下鼠标和键盘的模拟控制,也就是为手势和语音控制鼠标和键盘部分服务的. 有关于本系统构建的文章结构都会由三个部分来组织,一是该功能模块的介绍和在Linux下简单应用程序的实现:二是将该功能模块 ...
- uva 10518 - How Many Calls?(矩阵快速幂)
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...
- C++ 11 - STL - 函数对象(Function Object) (下)
1. 预定义函数对象 C++标准库内含许多预定义的函数对象,也就是内置的函数对象. 你可以充分利用他们,不必自己费心去写一些自己的函数对象. 要使用他们,你只要包含如下头文件 #include < ...
- 算法笔记_129:计数排序(Java)
目录 1 问题描述 2 解决方案 2.1比较计数排序 2.2 分布计数排序 1 问题描述 给定一组数据,请使用计数排序,得到这组数据从小到大的排序序列. 2 解决方案 2.1比较计数排序 下面算法 ...
- mysql c语言 动态链接库
下载地址 https://dev.mysql.com/downloads/connector/c/ 使用libmysql.lib和libmysql.dll进行操作mysql
- 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
- How to authenticate a user by uid and password?
原文地址:Authentication options | Basic authorization If you want to use simple binds with user DN and p ...