背景:

看了https://www.cnblogs.com/donlianli/p/10954716.html这篇文章之后,突然发现自己的知识面太窄了,连这些几乎可以说基础的工具都没怎么用过,于是决定了解一下。

embed jetty使用

1、引入依赖

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>7.6.9.v20130131</version>
</dependency>
<!--方便打印-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>

搜索了一下embed jetty相关的文章,没有发现太多,于是就引入了上面几个jar包。

2、代码

package me.lovegao.learn.jetty;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler; import com.alibaba.fastjson.JSONObject; public class JettyServerMain { public static void main(String[] args) throws Exception {
Server server = new Server(8888);
server.setHandler(new HelloHandler());
server.start();
server.join();
} } class HelloHandler extends AbstractHandler { @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
System.out.println("target:" + target
+ ",request:" + JSONObject.toJSONString(request.getParameterMap()));
long threadId = Thread.currentThread().getId();
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter(); out.println("hello+" + threadId); baseRequest.setHandled(true);
} }

  

3、执行

在浏览器请求:http://localhost:8888/hello/world?q1=1&q2=2

控制台打印如下:

target:/hello/world,request:{"q1":["1"],"q2":["2"]}

  

HttpClient使用

1、引入依赖

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>

来源:http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/dependency-info.html

2、参考官网代码

public static void request1() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8888/homepage?q1=v1");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
byte[] res = entity1.getContent().readAllBytes();
String str = new String(res);
System.out.println("str:" + str);
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
} finally {
response1.close();
}
httpclient.close();
}

3、执行

控制台打印结果:

HTTP/1.1 200 OK
str:hello+15

  

4、HttpPost请求

 import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; public class HttpPost { public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://ip:port/fe");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "str"));
nvps.add(new BasicNameValuePair("feature", "address"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response1 = httpclient.execute(httpPost);
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
byte[] res = new byte[1024];
InputStream is = entity1.getContent();
int len = 0;
while((len = is.read(res)) > 0) {
String str = new String(res, 0, len);
System.out.print(str);
}
EntityUtils.consume(entity1);
} finally {
response1.close();
}
httpclient.close();
} }

jetty+httpClient使用的更多相关文章

  1. Jetty + HttpClient 处理http请求

    本人最近通过自己动手处理http请求,对http协议.Jetty以及HttpClient有了更深刻的理解,特在此与大家分享. 此图是http协议的请求格式.根据请求方法,有get和post之分.get ...

  2. 【转】Spring websocket 使用

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html https://spr ...

  3. HttpClient相关

    HTTPClient的主页是http://jakarta.apache.org/commons/httpclient/,你可以在这里得到关于HttpClient更加详细的信息 HttpClient入门 ...

  4. Jetty使用教程(四:21-22)—Jetty开发指南

    二十一.嵌入式开发 21.1 Jetty嵌入式开发HelloWorld 本章节将提供一些教程,通过Jetty API快速开发嵌入式代码 21.1.1 下载Jetty的jar包 Jetty目前已经把所有 ...

  5. jetty 最后版本类库树, 基本上大多数应用都够了

    d:\jetty-distribution-8.1.17.v20150415\lib\annotations\javax.annotation-1.1.0.v201108011116.jarjavax ...

  6. 嵌入式jetty的HTTP实现

    2    嵌入式jetty的HTTP实现 布拉君君 2.1 简单HTTP实现 2.1.1 HTTP SERVER端实现 2.1.1.1 HTTP SERVER端实现概述 在代码中嵌入一个Jetty s ...

  7. Jetty开发指导:HTTP Client

    介绍 Jetty HTTP client模块提供易用的API.工具类和一个高性能.异步的实现来运行HTTP和HTTPS请求. Jetty HTTP client模块要求Java版本号1.7或者更高,J ...

  8. Message高级特性 & 内嵌Jetty实现文件服务器

    1. Messaage Properties  常见属性 更多的属性以及介绍参考:http://activemq.apache.org/activemq-message-properties.html ...

  9. jetty 客服端 与服务端

    jetty 服务端,客服端有请求buffter 检查 默认4kb 4096 客服端 HttpClient client=new HttpClient(); client.setRequestBuffe ...

随机推荐

  1. 在Win7中修改 系统盘中 “系统” - “用户” 的环境变量映射关系

    1.在此列表中,选中对应登录帐号 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList 2.将Prof ...

  2. luogu1220 关路灯

    题目大意 路面上有一些开着的灯,每个灯有功率和它的位置,人在第c个灯处,行走速度1m/s.问怎样关灯能使耗能最小,输出这个耗能. 思路 #include <cstdio> #include ...

  3. win7下code::blocks开发环境

    一.环境配置步骤: 下载安装code::blocks; 下载安装MinGW; 在complier settings中, Toolchain executables选择MinGW的安装路径. 完成安装. ...

  4. iOS版本、iPhone版本、Xcode版本比对

    iOS版本 iPhone版本 Xcode版本 其他 2003年 Xcode1.0 2005年4月29日 Xcode2.0 2007年1月9日 iPhone OS(iOS1): 虚拟键盘.谷歌地图 第一 ...

  5. hdu1429胜利大逃亡(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. 【Usaco2006Mar】Milk Team Select产奶比赛

    [思路分析] 比赛的时候想到了用我确实也想到了树形DP,但是状态没有确定对,连样例都没有过 PS:这是第二道发现还可以用状态作为答案最后输出的题目 正解:树形DP(背包) 按照读进来的数据,我们先建一 ...

  7. java反射机制学习小结

    之前一直对java的反射机制理解得很模糊,今天因为学习spring,所以花了些时间总算把它理顺了,记录一下 另外,推荐读读这篇文章,写的挺好的http://blog.csdn.net/woshixuy ...

  8. ACM_城市交通线(简单并查集)

    城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...

  9. html中canvas渲染图片,并转化成base64格式保存

    最近在做一个上传头像然后保存显示的功能,因为涉及到裁剪大小和尺寸比例,所以直接上传图片再展示的话,就会出现问题,所以就想用canvas来渲染裁剪后的图片,然后转化成base64格式的图片再存储,这样取 ...

  10. poj1101 the game 广搜

    题目大意: 类似于连连看,问从起点到终点最少需要几条线段. 规则: 1.允许出界. 2.空格的地方才能走. 分析: 题目做下来发现没有卡时间,所以主要还是靠思路.也就是说不用考虑离线算法.直接以每个起 ...