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

📄 httpconnection.java

📁 封装了SQL、Socket、WAP、MIME等功能的通用组件
💻 JAVA
字号:
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author 刘学
 * @version 1.0
 */
package org.lazybug.net;

import org.lazybug.util.*;
import java.io.*;
import java.net.*;

public class HTTPConnection extends Connection
{
    private int bufferLength = 0;
    private URL url = null;
    private URLConnection httpUrlConnection = null;
    public int contentLength;

    public HTTPConnection(int nPageNum, int nPageSize) throws Exception
    {
        super(nPageNum, nPageSize);
        bufferLength = this.pageNum * this.pageSize;
        super.buffer = new byte[bufferLength];
        url = new URL( "http://"+ConfigUtil.getString("_http_proxy_ip")+":"+
                       ConfigUtil.getString("_http_proxy_port")+"/httpproxy" );
    }

    public int getMaxPageLength()
    {
        return this.pageSize;
    }

    public int getBufferLength()
    {
        return bufferLength;
    }

    public int onReceiveFrom(Packet packet) throws java.io.IOException
    {
        DataInputStream in = new DataInputStream(httpUrlConnection.getInputStream());

        int nLength1, nLength2 = 0;
        nLength1 = in.read(buffer, packet.offset, 2);

        if( this.contentLength == httpUrlConnection.getContentLength() )
        {
//            Log.getInstance().write("读数据包[长度:"+httpUrlConnection.getContentLength()+"]结束");
            in.close();
            this.contentLength = 0;
            return -1;
        }

        if( nLength1 != 2 )
        {
            in.close();
            return -1;
        }
        byte[] buf = new byte[2];
        buf[0] = buffer[packet.offset];
        buf[1] = buffer[packet.offset+1];
        nLength1 = Tools.bytesToInt( buf );
        if( nLength1 > this.getMaxPageLength() ||
            nLength1 < packet.getHeaderMaxLength() )
        {
            in.close();
            return -1;
        }

        nLength2 = in.read(buffer, packet.offset + 2, nLength1 - 2);

        if( nLength2 != nLength1-2 )
        {
            in.close();
            packet.setLength(-1);
            return -1;
        }

        this.contentLength += nLength1;

        return nLength1;
    }

    public int onSendTo(Packet packet) throws java.io.IOException
    {
        httpUrlConnection = null;
        httpUrlConnection = url.openConnection();

        if (!httpUrlConnection.getDoOutput()) httpUrlConnection.setDoOutput(true);
        if (!httpUrlConnection.getDoInput()) httpUrlConnection.setDoInput(true);

        DataOutputStream out = null;
        out = new DataOutputStream(httpUrlConnection.getOutputStream());
        out.write(buffer, packet.buffer_offset, packet.getLength());
        out.flush();
        out.close();
        return packet.getLength();
    }
}

⌨️ 快捷键说明

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