⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpsocket.java

📁 网络蜘蛛源码发展套件
💻 JAVA
字号:
package com.heaton.bot;import java.net.*;import java.io.*;public class HTTPSocket extends HTTP {    private static String proxyHost;    private static int proxyPort;    private static String proxyUID;    private static String proxyPWD;        public void writeString(OutputStream out,String str)    throws java.io.IOException {        out.write( str.getBytes() );        out.write( "\r\n".getBytes() );        Log.log(Log.LOG_LEVEL_TRACE,"Socket Out:" + str );    }        synchronized public void lowLevelSend(String url,String post)    throws java.net.UnknownHostException, java.io.IOException {        String command;// What kind of send POST or GET        StringBuffer headers;// Used to hold outgoing client headers        byte buffer[]=new byte[1024];//A buffer for reading        int l,i;// Counters        Attribute a;// Used to process incoming attributes        int port=80;// What port, default to 80        boolean https = false;// Are we using HTTPS?        URL u;// Used to help parse the url parameter        Socket socket = null;        OutputStream out = null;        InputStream in = null;                // parse the URL        try {            if ( url.toLowerCase().startsWith("https") ) {                url = "http" + url.substring(5);// so it can be parsed                u = new URL(url);                if ( u.getPort()==-1 )                    port=443;                https = true;            } else                u = new URL(url);                        if ( u.getPort()!=-1 )                port = u.getPort();                        // connect            if( useProxy() ) {                Log.log(Log.LOG_LEVEL_NORMAL,"Connection to: " + proxyHost+"(" + proxyPort + ")" );                try                {                    socket = new Socket(proxyHost,proxyPort);                }                catch(IOException e)                {                    throw new IOException("Proxy connect failed: " + e.getMessage() );                }                                if( https ) {                    socket.setSoTimeout(timeout);                    out = socket.getOutputStream();                    in = socket.getInputStream();                                        String str = "CONNECT " + u.getHost() + ":" + port + " HTTP/1.0";                    Log.log(Log.LOG_LEVEL_NORMAL,"Tunnel: " + str );                    writeString(out,str);                    str = "User-Agent: "+agent;                    writeString(out,str);                    writeString(out,"");                    out.flush();                    do {                        str = receive(in);                        Log.log(Log.LOG_LEVEL_TRACE,"Tunnel handshake: " + str );                    } while(str.length()>0);                                        //in.close();                    //out.close();                    socket = SSL.getSSLSocket(socket,u.getHost(),port);                    out = socket.getOutputStream();                    in = socket.getInputStream();                }            }            else {                if( https )                    socket = SSL.getSSLSocket(u.getHost(),port);                else                    socket = new Socket(u.getHost(),port);            }                        socket.setSoTimeout(timeout);            out = socket.getOutputStream();            in = socket.getInputStream();                        // send command, i.e. GET or POST            if ( post == null )                command = "GET ";            else                command = "POST ";                        String file = u.getFile();            if ( file.length()==0 )                file="/";                        // proxy support                        if( useProxy()) {                addProxyAuthHeader();                if(port!=80)                    file = "http://" + u.getHost() + ":" + port + file;                else                    file = "http://" + u.getHost() + file;            }                        // end proxy support                        command = command + file + " HTTP/1.0";            writeString(out,command);            Log.log(Log.LOG_LEVEL_NORMAL,"Request: " + command );                        // Process client headers                        if ( post!=null )                clientHeaders.set("Content-Length","" + post.length());                        clientHeaders.set("Host", u.getHost() );                        i=0;            headers = new StringBuffer();            do {                a = clientHeaders.get(i++);                if ( a!=null ) {                    headers.append(a.getName());                    headers.append(": ");                    headers.append(a.getValue());                    headers.append("\r\n");                    Log.log(Log.LOG_LEVEL_TRACE,"Client Header:" + a.getName() + "=" + a.getValue() );                }            } while ( a!=null );                        Log.log(Log.LOG_LEVEL_DUMP,"Writing client headers:" + headers.toString() );            if ( headers.length()>=0 )                out.write(headers.toString().getBytes() );                        // Send a blank line to signal end of HTTP headers            writeString(out,"");            if ( post!=null ) {                Log.log(Log.LOG_LEVEL_TRACE,"Socket Post(" + post.length() + " bytes):" + new String(post) );                out.write( post.getBytes()  );            }                        /* Read the result */            /* First read HTTP headers */                        header.setLength(0);            int chars = 0;            boolean done = false;                        while ( !done ) {                int ch;                                ch = in.read();                if ( ch==-1 )                    done=true;                                switch ( ch ) {                    case '\r':                        break;                    case '\n':                        if ( chars==0 )                            done =true;                        chars=0;                        break;                    default:                        chars++;                        break;                }                                header.append((char)ch);            }                        // now parse the headers and get content length            parseHeaders();            Attribute acl = serverHeaders.get("Content-length");            int contentLength=0;            try {                if ( acl!=null )                    contentLength = Integer.parseInt(acl.getValue());            } catch ( Exception e ) {                Log.logException("Bad value for content-length:",e);            }                        int max;            if ( maxBodySize!=-1 )                max = Math.min(maxBodySize,contentLength );            else                max = contentLength;                        if ( max<1 )                max=-1;                        ByteList byteList = new ByteList();            byteList.read(in,max);            body = byteList.detach();            Log.log(Log.LOG_LEVEL_DUMP,"Socket Page Back:" + body + "\r\n" );                        if ( (err>=400) && (err<=599) ) {                Log.log(Log.LOG_LEVEL_ERROR,"HTTP Exception:" + response );                throw new HTTPException(response);            }                    }                // Cleanup        finally {            if ( out!=null ) {                try {                    out.close();                } catch ( Exception e ) {                }            }                        if ( in!=null ) {                try {                    in.close();                } catch ( Exception e ) {                }            }                        if ( socket!=null ) {                try {                    socket.close();                } catch ( Exception e ) {                }            }        }    }        HTTP copy() {        return new HTTPSocket();    }        public static void setProxyHost(String proxyHost) {        HTTPSocket.proxyHost = proxyHost;    }        public static void setProxyUID(String proxyUID) {        HTTPSocket.proxyUID = proxyUID;    }        public static void setProxyPWD(String proxyPWD) {        HTTPSocket.proxyPWD = proxyPWD;    }        public static void setProxyPort(int id) {        HTTPSocket.proxyPort = id;    }        public static String getProxyHost() {        return proxyHost;    }        public static String getProxyUID() {        return proxyUID;    }        public static String getProxyPWD() {        return proxyPWD;    }        public static int getProxyPort() {        return proxyPort;    }        public static boolean useProxy() {        return( (proxyHost!=null) && (proxyHost.length()>0) );    }        /**     * This method is called to add the user authorization headers     * to the HTTP request.     */    protected void addProxyAuthHeader() {        if( (proxyUID!=null) && (proxyUID.length()>0) ) {            String hdr = proxyUID + ":" + proxyPWD==null?"":proxyPWD;            String encode = URLUtility.base64Encode(hdr);            clientHeaders.set("Proxy-Authorization","Basic " + encode );        }    }        protected String receive(InputStream in)    throws IOException {        String result = "";        boolean done = false;                while (!done) {            int ch = in.read();            switch (ch) {                case 13:                    break;                case 10:                    done=true;                    break;                default:                    result+=(char)ch;            }                    }        //System.out.println("Recv:" + result );        return result;    }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -