背景:

看了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. mac Homebrew Updating慢,替换及重置Homebrew默认源

    替换成清华的镜像: https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git

  2. luogu3155 [CQOI2009]叶子的染色

    题目大意 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方案应该保证根结点到每个叶子的简单路径上都至少包含一个有色结点( ...

  3. JAVA8与lambda表达式

    一.lambda表达式的来源 Lambda 表达式(拉姆达表达式)来源于lambda演算是Alonzo Church给出的一套图灵机等价的形式计算系统.lambda演算系统以函数和变量为基础,也可以进 ...

  4. web认证方案

    web构建在http之上,而它又是无状态协议,如何控制用户访问服务器上的受限资源呢? 最原始你想法通过http基本认证,每次发请求时都向后台传递用户名密码信息,服务器每次收到请求后都先验证用户是否合法 ...

  5. runC爆严重安全漏洞,主机可被攻击!使用容器的快打补丁

    runC 是 Docker,Kubernetes 等依赖容器的应用程序的底层容器运行时.此次爆出的严重安全漏洞可使攻击者以 root 身份在主机上执行任何命令. 容器的安全性一直是容器技术的一个短板. ...

  6. go函数初级

    一.简介 在go语言中,函数的功能是非常强大的,以至于被认为拥有函数式编程语言的多种特性. 二.介绍 1.一个程序中包含了很多的函数:函数式基本的代码块 2.函数编写的顺序是无关紧要的:鉴于可读性的需 ...

  7. Oracle_exp/expdp备份

    目录索引 1.exp和expdp的区别 2.expdp导出数据库流程 一.↓↓exp和expdp的区别↓↓ 1.exp和expdp最明显的区别就是导出速度的不同.expdp导出是并行导出(如果把exp ...

  8. JQuery+Bootstrap总结

    ================JQuery=========== JQuery 1. jQuery是什么? 一个js插件, 相比较原生的DOM操作更简单.开发效率更高 2. jQuery使用 1. ...

  9. XOJ测试 2016.5.22

    哈哈 我是最先使用XOJ的人之一 膜拜zrt ing 首先是XOJ神奇的界面 还没有建设完的OJ是这个样子的 一共有5道题 这次小测有3道题 是T2T3T4 首先是骑士精神 (BZOJ1085) 上来 ...

  10. SQL Server跨库跨服务器访问实现

    我们经常会遇到一个数据库要访问另一个数据库,或者一台服务器要访问另一台服务器里面的数据库. 那么这个如何实现的呢? 相信看完这篇文章你就懂了! 同一台服务器跨库访问实现 1. 首先创建两个数据库Cro ...