资源简介

项目需要,一直在学习http相关知识,动手写了一个http服务器的实例,通过该实例可以清楚了解整个http包头及工作原理;(直接能eclipse运行)

资源截图

代码片段和文件信息

package com.cnryb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.sun.net.httpserver.Headers;

public class HttpC {

/**
 * @param args
 * @throws IOException
 * @throws IllegalStateException
 */
public static void main(String[] args) throws IllegalStateException
IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost=new HttpPost(“http://127.0.0.1:8765“);
HttpResponse response=httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
parseResponseEntity(response);
if (entity != null) {
InputStream instreams = entity.getContent();
BufferedReader bf = new BufferedReader(new InputStreamReader(
instreams));
StringBuilder sb=new StringBuilder();
String line = null;
while ((line = bf.readLine()) != null) {
sb.append(line + “\n“);
}
System.out.println(“\nContent:“+sb.toString()+“length=“+sb.length());
//httpPost.abort();
}
}

private static void parseResponseEntity(HttpResponse response) {
if (response != null) {
Header[] headers=response.getAllHeaders();
System.out.println(“Headers:“+“size=“+headers.length);
for(Header header:headers){
System.out.println(“Name=“+header.getName());
System.out.println(“Value=“+header.getValue());
System.out.println(“\n“);

}
System.out.println(“\n“+“Version=“+response.getProtocolVersion()+“\nStatus=“+response.getStatusLine().getStatusCode());

System.out.println(“Local=“+response.getLocale().toString());

HttpEntity entity = response.getEntity();
//entity.

}

}

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2657  2014-07-22 16:56  HttpServerAndClient\HttpSer\.classpath

     文件        395  2014-07-23 10:32  HttpServerAndClient\HttpSer\.project

     文件        598  2013-06-26 16:19  HttpServerAndClient\HttpSer\.settings\org.eclipse.jdt.core.prefs

     文件       3296  2014-07-23 10:32  HttpServerAndClient\HttpSer\bin\com\cnryb\HttpC.class

     文件       1948  2014-07-23 10:32  HttpServerAndClient\HttpSer\bin\com\cnryb\HttpSer$MyResponseHandler.class

     文件       1074  2014-07-23 10:32  HttpServerAndClient\HttpSer\bin\com\cnryb\HttpSer.class

     文件     188671  2014-06-24 17:32  HttpServerAndClient\HttpSer\lib\commons-beanutils-1.7.0.jar

     文件     232771  2013-06-26 16:42  HttpServerAndClient\HttpSer\lib\commons-codec-1.6.jar

     文件     263965  2014-06-24 16:35  HttpServerAndClient\HttpSer\lib\commons-codec-1.9.jar

     文件     163151  2013-10-17 21:44  HttpServerAndClient\HttpSer\lib\commons-io-2.1.jar

     文件      60686  2013-06-26 16:45  HttpServerAndClient\HttpSer\lib\commons-logging-1.1.1.jar

     文件      21776  2013-06-26 16:45  HttpServerAndClient\HttpSer\lib\fluent-hc-4.2.5.jar

     文件     433071  2013-10-17 21:44  HttpServerAndClient\HttpSer\lib\httpclient-4.2.3.jar

     文件     433368  2013-06-26 16:42  HttpServerAndClient\HttpSer\lib\httpclient-4.2.5.jar

     文件     115625  2013-06-26 16:45  HttpServerAndClient\HttpSer\lib\httpclient-cache-4.2.5.jar

     文件     227275  2013-06-26 16:45  HttpServerAndClient\HttpSer\lib\httpcore-4.2.4.jar

     文件     282160  2014-06-24 16:55  HttpServerAndClient\HttpSer\lib\httpcore-4.3.jar

     文件      26477  2014-06-24 16:56  HttpServerAndClient\HttpSer\lib\httpmime-4.2.3.jar

     文件      26477  2013-06-26 16:45  HttpServerAndClient\HttpSer\lib\httpmime-4.2.5.jar

     文件      17289  2013-10-17 21:44  HttpServerAndClient\HttpSer\lib\jcl-over-slf4j-1.6.6.jar

     文件     152797  2014-07-03 11:20  HttpServerAndClient\HttpSer\lib\jdom.jar

     文件      39308  2014-06-30 11:48  HttpServerAndClient\HttpSer\lib\json.org.jar

     文件     743361  2009-05-28 05:28  HttpServerAndClient\HttpSer\lib\jxl.jar

     文件      22545  2014-06-24 16:59  HttpServerAndClient\HttpSer\lib\noggit-0.5.jar

     文件      28688  2014-06-24 16:35  HttpServerAndClient\HttpSer\lib\slf4j-api-1.7.6.jar

     文件       4090  2014-04-04 13:11  HttpServerAndClient\HttpSer\lib\slf4j-nop-1.7.7.jar

     文件     383972  2013-10-17 21:44  HttpServerAndClient\HttpSer\lib\solr-solrj-4.5.1.jar

     文件        396  2014-07-03 17:08  HttpServerAndClient\HttpSer\MANIFEST.MF

     文件    2512124  2014-07-22 16:55  HttpServerAndClient\HttpSer\poi.jar

     文件       2020  2014-07-22 17:01  HttpServerAndClient\HttpSer\src\com\cnryb\HttpC.java

............此处省略14个文件信息

评论

共有 条评论