java 實現Http代理 (1)

來源:互聯網
上載者:User

 java 實現Http代理的關鍵是將接收到的http請求轉寄到被代理的主機,然後將主機的返回資料再轉寄到客戶機

關鍵點:
1. HTTP協議

2. java 阻塞IO

3. 使用java阻塞IO實現轉寄HTTP請求的一個關鍵點就是轉寄站必須能夠正確分析收到的HTTP請求內容,並根據請求內容正確的轉寄。

下面是一段請求內容:-----------------------------------------------------之間的是真正的請求內容,以/r/n為行結束符
-----------------------------------------------------
GET /test/test.jsp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: localhost:7272
Connection: Keep-Alive

-----------------------------------------------------
當java的io讀取到請求尾的時候,由於阻塞IO,導致讀取等待,這個時候,轉寄站,必須能夠解析上面的內容,根據Content-Length決定讀取到什麼地方結束,如果沒有Content-Length,那麼Content-Length為0,也就是不讀取。
為了更便於對該請求進行操作,我們封裝出來一個簡單的HttpRequest對象:

/* * $RCSfile: HttpRequest.java,v $$ * $Revision: 1.1  $ * $Date: 2007-4-15  $ * * Copyright (C) 2008 Skin, Inc. All rights reserved. * * This software is the proprietary information of Skin, Inc. * Use is subject to license terms. */package com.skin.taurus.http;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;/** * <p>Title: HttpRequest</p>  * <p>Description: </p>  * @author xuesong.net * @version 1.0 */public class HttpRequest{    private String method;    private String requestURI;    private String requestURL;    private String queryString;    private String httpProtocol;    private String localAddr;    private int serverPort = -1;    private String remoteAddr;    private String remoteHost;    private int remotePort = -1;    private String characterEncoding;    private HttpHeader httpHeader;    private InputStream inputStream;    private OutputStream outputStream;    public HttpRequest()    {        this.httpHeader = new HttpHeader();    }    public String getContextPath()    {        return "/";    }    /**     * @param encoding     */    public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException    {        this.characterEncoding = encoding;    }    /**     * @return String     */    public String getCharacterEncoding()    {        return this.characterEncoding;    }    /**     * @return HttpHeader     */    public void setHttpHeader(HttpHeader httpHeader)    {        this.httpHeader = httpHeader;    }    /**     * @return HttpHeader     */    public HttpHeader getHttpHeader()    {        return this.httpHeader;    }    /**     * @param name     * @param value     */    public void addHeader(String name, String value)    {        this.httpHeader.addHeader(name, value);    }    /**     * @param name     * @param value     */    public void setHeader(String name, String value)    {        this.httpHeader.setHeader(name, value);    }    /**     * @param name     * @return - String     */    public String getHeader(String name)    {        return this.httpHeader.getHeader(name);    }    /**     * @param name     * @return - String     */    public String[] getHeaderValues(String name)    {        return this.httpHeader.getHeaderValues(name);    }    /**     * @return - String     */    public String getHttpProtocol()    {        return httpProtocol;    }    /**     * @param httpProtocol     */    public void setHttpProtocol(String httpProtocol)    {        this.httpProtocol = httpProtocol;    }    /**     *      * @return - String     */    public String getMethod()    {        return method;    }    /**     * @param method     */    public void setMethod(String method)    {        this.method = method;    }    /**     * @return - String     */    public String getQueryString()    {        return queryString;    }    /**     *      * @param queryString     */    public void setQueryString(String queryString)    {        this.queryString = queryString;    }    /**     *      * @return - String     */    public String getRequestURI()    {        return requestURI;    }    /**     *      * @param requestURI     */    public void setRequestURI(String requestURI)    {        this.requestURI = requestURI;    }    /**     *      * @return - String     */    public String getRequestURL()    {        return requestURL;    }    /**     *      * @param requestURL     */    public void setRequestURL(String requestURL)    {        this.requestURL = requestURL;    }    /**     *      * @return - String     */    public String getContentType()    {        return (String)(this.getHeader("Content-Type"));    }    /**     *      * @return - int     */    public int getContentLength()    {        String value = (String)(this.getHeader("Content-Length"));        int contentLength = -1;        if(value == null)        {            return contentLength;        }        try        {            contentLength = Integer.parseInt(value);        }        catch(NumberFormatException e)        {        }        return contentLength;    }    public String getLocalAddr()    {        return localAddr;    }    public void setLocalAddr(String localAddr)    {        this.localAddr = localAddr;    }    public int getServerPort()    {        return serverPort;    }    public void setServerPort(int serverPort)    {        this.serverPort = serverPort;    }    public String getRemoteAddr()    {        return remoteAddr;    }    public void setRemoteAddr(String remoteAddr)    {        this.remoteAddr = remoteAddr;    }    public String getRemoteHost()    {        return remoteHost;    }    public void setRemoteHost(String remoteHost)    {        this.remoteHost = remoteHost;    }    public int getRemotePort()    {        return remotePort;    }    public void setRemotePort(int remotePort)    {        this.remotePort = remotePort;    }    /**     * @return - InputStream     */    public InputStream getInputStream()    {        return inputStream;    }    /**     * @param inputStream     */    public void setInputStream(InputStream inputStream)    {        this.inputStream = inputStream;    }    /**     * @return - OutputStream     */    public OutputStream getOutputStream()    {        return outputStream;    }    /**     * @param outputStream     */    public void setOutputStream(OutputStream outputStream)    {        this.outputStream = outputStream;    }    /**     * @see java.lang.Object#toString()     */    public String toString()    {        StringBuilder buffer = new StringBuilder();        buffer.append(this.method).append(" ").append(this.getRequestURL()).append(" ").append(this.getHttpProtocol()).append("\r\n");        buffer.append(this.httpHeader.toString());        buffer.append("\r\n");        return buffer.toString();    }}


完整的工程源碼下載地址: https://github.com/xuesong123

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.