在Java领域,谈到网络编程,如果是NB人物,脑海里第一反应就是MINA,NETTY,GRIZZLY等优秀的开源框架。
上面看看而已,对于刚接触网络编程的人来说,最原始最底层的才是最重要的.自然最先的是java.net.*类库.HTTP协议方面的知识.
然后Apache的HttpComponents很不错吧,用的人很多,现在我学的就是HttpComponents.
Apache的各个技术的新旧影响觉得不是非常大,没JBOSS的一些技术换个版本技术感觉大变样一样.各个类名都变化无常的.所以我也经常吐槽JBOSS那帮家伙就是喜欢吹牛逼.现在的公司一帮子东西都是JBOSS...我说实话感觉头大.其实最主要还是自己英语垃圾....哎.....跑题了.
HttpComponents已经是Apache的顶级项目了,它旨在为我们提供一个Http协议相关的Java平台工具集。超文本传输协议 (HTTP) 也许是今天在因特网上使用的最重要协议。HttpComponents就专门搞这个的.
HttpComponents 结构
1.HttpComponents Core
2.HttpComponents Client
3.HttpComponents AsyncClien
1.1 HTTP 消息
1.结构第一部分叫Request line, 第二部分叫Request header, 第三部分是body. header和body之间有个空行.
HttpRequest request = new BasicHttpRequest("GET", "/",HttpVersion.HTTP_1_1); System.out.println(request.getRequestLine().getMethod()); System.out.println(request.getRequestLine().getUri()); System.out.println(request.getProtocolVersion()); System.out.println(request.getRequestLine().toString());
输出 GET / HTTP/1.1 GET / HTTP/1.1
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK, "OK"); System.out.println(response.getProtocolVersion()); System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString()); 输出: HTTP/1.1 200 OK HTTP/1.1 200 OK
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie","c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie","c2=b; path=\"/\", c3=c; domain=\"localhost\""); Header h1 = response.getFirstHeader("Set-Cookie"); System.out.println(h1); Header h2 = response.getLastHeader("Set-Cookie"); System.out.println(h2); Header[] hs = response.getHeaders("Set-Cookie"); System.out.println(hs.length); Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost" 2
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie","c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie","c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderIterator it = response.headerIterator("Set-Cookie"); while (it.hasNext()) { System.out.println(it.next()); } Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie","c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie","c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Set-Cookie")); while (it.hasNext()) { HeaderElement elem = it.nextElement(); System.out.println(elem.getName() + " = " + elem.getValue()); NameValuePair[] params = elem.getParameters(); for (int i = 0; i < params.length; i++) { System.out.println(" " + params[i]); } } c1 = a path=/ domain=localhost c2 = b path=/ c3 = c domain=localhost
StringEntity myEntity = new StringEntity("important message", "UTF-8"); System.out.println(myEntity.getContentType()); System.out.println(myEntity.getContentLength()); System.out.println(EntityUtils.getContentCharSet(myEntity)); System.out.println(EntityUtils.toString(myEntity)); System.out.println(EntityUtils.toByteArray(myEntity).length); 输出: Content-Type: text/plain; charset=UTF-8 17 UTF-8 important message 17
HttpResponse response; HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // do something useful } finally { instream.close(); } }请注意HttpEntity#writeTo(OutputStream)方法亦须确保适当释放的系统资源,一旦该实体已完全写出。如果此方法通过调用HttpEntity#getContent()获得java.io.InputStream的实例,则它还预计关闭该流 finally 子句。
一个需要设置的内容的流,和 (可选)长度。这可以分别用BasicHttpEntity#setContent(InputStream)和BasicHttpEntity#setContentLength(long)的方法进行。
BasicHttpEntity myEntity = new BasicHttpEntity(); myEntity.setContent(someInputStream); myEntity.setContentLength(340); // sets the length to 340
String myData = "Hello world on the other side!!"; ByteArrayEntity myEntity = new ByteArrayEntity(myData.getBytes());
StringBuilder sb = new StringBuilder(); Map<String, String> env = System.getenv(); for (Entry<String, String> envEntry : env.entrySet()) { sb.append(envEntry.getKey()).append(": ") .append(envEntry.getValue()).append("\n"); } // construct without a character encoding (defaults to ISO-8859-1) HttpEntity myEntity1 = new StringEntity(sb.toString()); // alternatively construct with an encoding (mime type defaults to "text/plain") HttpEntity myEntity2 = new StringEntity(sb.toString(), "UTF-8"); // alternatively construct with an encoding and a mime type HttpEntity myEntity3 = new StringEntity(sb.toString(), "text/html", "UTF-8");
InputStream instream = getSomeInputStream(); InputStreamEntity myEntity = new InputStreamEntity(instream, 16);
HttpEntity entity = new FileEntity(staticFile,"application/java-archive");
这使得它可以让一个可重复的实体,从一个不可重复的实体。如果提供的实体已经是可重复的,调用只是传递到底层的实体.
myNonRepeatableEntity.setContent(someInputStream); BufferedHttpEntity myBufferedEntity = new BufferedHttpEntity(myNonRepeatableEntity);