java 写的能够响应浏览器请求的 http 服务器

这只是一个小Demo,话几十分钟搞出来的。

不废话先上代码。

首先是服务端的

package com.cnryb;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import org.apache.http.HttpStatus;

import com.sun.net.httpserver.*;

public class HttpSer {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(
                "127.0.0.1", 8765), 0);
        server.createContext("/",new MyResponseHandler());
        server.setExecutor(null); // creates a default executor
        server.start();

    }
    public static class MyResponseHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            //针对请求的处理部分     
            //返回请求响应时,遵循HTTP协议  
            String responseString = "<font color='#ff0000'>Hello! This a HttpServer!</font>"; 
            //设置响应头  
            httpExchange.sendResponseHeaders(HttpStatus.SC_OK, responseString.length());    
            OutputStream os = httpExchange.getResponseBody();    
            os.write(responseString.getBytes());    
            os.close();  
        }
    }

}

容易出现问题的是端口被占用,解决办法

换一个端口,嘿嘿

 

然后是用java访问该服务器的代码

package com.cnryb;

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

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;

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");
//        HttpGet httpgets = new HttpGet("http://127.0.0.1:8765");
//        HttpResponse response = httpclient.execute(httpgets);
        HttpResponse response=httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        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(sb.toString());
//            httpgets.abort();
            httpPost.abort();
        }
    }

}

这个段代码不只是能够访问这一个服务,别的已经存在的服务器也能够范围,像百度神马的,简单来说就是模拟浏览器请求的

写这些东西的时候,唯一让我纠结的就是那些该死的jar包,为了不让亲们纠结,我把我写好的demo也放上来,里面有jar包的

Live Writer竟然不能放附件??等会儿手动上传吧

 

源代码点这里下载

 

    原文作者:任跃兵
    原文地址: http://www.cnblogs.com/cnryb/archive/2013/06/27/3158027.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞