jetty+httpClient使用
背景:
看了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请求
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.apache.http.HttpEntity; 7 import org.apache.http.NameValuePair; 8 import org.apache.http.client.ClientProtocolException; 9 import org.apache.http.client.entity.UrlEncodedFormEntity; 10 import org.apache.http.client.methods.CloseableHttpResponse; 11 import org.apache.http.client.methods.HttpPost; 12 import org.apache.http.impl.client.CloseableHttpClient; 13 import org.apache.http.impl.client.HttpClients; 14 import org.apache.http.message.BasicNameValuePair; 15 import org.apache.http.util.EntityUtils; 16 17 public class HttpPost { 18 19 public static void main(String[] args) throws ClientProtocolException, IOException { 20 CloseableHttpClient httpclient = HttpClients.createDefault(); 21 HttpPost httpPost = new HttpPost("http://ip:port/fe"); 22 List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 23 nvps.add(new BasicNameValuePair("name", "str")); 24 nvps.add(new BasicNameValuePair("feature", "address")); 25 httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 26 CloseableHttpResponse response1 = httpclient.execute(httpPost); 27 try { 28 System.out.println(response1.getStatusLine()); 29 HttpEntity entity1 = response1.getEntity(); 30 byte[] res = new byte[1024]; 31 InputStream is = entity1.getContent(); 32 int len = 0; 33 while((len = is.read(res)) > 0) { 34 String str = new String(res, 0, len); 35 System.out.print(str); 36 } 37 EntityUtils.consume(entity1); 38 } finally { 39 response1.close(); 40 } 41 httpclient.close(); 42 } 43 44 }