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竟然不能放附件??等會兒手動上傳吧

 

源代碼點這里下載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM