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

📄 netlayer.java

📁 一个用 java写的wap浏览器 对于浏览器感兴趣起的可以看看咯
💻 JAVA
字号:
/*
 * j2wap is distributed under the terms of the GNU Public License
 *
 *  j2wap was written by:
 *
 *	        Scott Campbell
 *			Michael Nordling
 *			Karl Maftoum
 * 			Julian Bright
 *
 *			This was a final project for Comp.Engineering at the University of Canberra, Australia
 *
 *			Now released as Open Source software. 28 November 2000
 *
 *			Email: k_maftoum@hotmail.com for more info
 *
 *			Use entirely at your own risk.
 */

/**
 * Title:       NetLayer
 * Description: The NetLayer provdides all of the net services to j2wap
 * Company:     J2wap.com
 * @author      Karl Maftoum
 * @version     1.1
 */




/*
   This is the UDP NetLayer for j2wap
   Original Version
   GPL 20/8/00 Karl Maftoum
 */


/* This is the version for j2me */


/* Revision History

	Version 0.0.1 20/8/00

	Version 0.0.2 24/8/00

	* Massive code cleanup
	* Setup gateway and URI strings
	* Working Content (Finally)

	Version 0.0.3 27/8/00

	* POST code added

	Version 0.0.4 4/9/00

	* Implementing Timeout
	* All encoding accept bytes etc now work
	* commented out Accept wbmp (not implemented yet)

	Version 0.0.5 7/9/00

	* Total re-write to implement POST & GET as one function for size
	* Works except I can't yet test POST (no sites)
	* IP Layer Header Deleted from Byte Array

	Version 0.0.6 19/09/00

	* Changed to add gc() calls, and less array copies

	Version 0.0.7  2/10/00

	* Removed debugging System.out.println calls
	* Adding threading & exception handling for UI Cancel
	* Getting the timeout to work properly

	Version 0.0.8  6/10/00

	* Fix the http:// substitution to work correctly
	* Removed excessive gc() calls
	* UI Cancel still doesn't work
	* TID Now handled properly

	Version 0.0.9  17/11/00

	* Fixed all the bits from the Windows port back to Palm
	* Got all redirects/timeouts/wierd things working.
	* Removed UI Cancel threading, just didn't work properly
	* This is the final version I think

	Version 0.1.0 18/11/00

	* Removed all debugging code
	* Code Styling, and formatting

	Versoin 1.1 22/11/00

	* Final Version
*/

package net;
import java.io.IOException;
import java.io.InterruptedIOException;
import javax.microedition.io.*;

public class NetLayer
{
  static int TID = 2;
  String httpsub;
  String FinalURI;
  public wae.CurrentDeck c_clsCurrDeck;
  boolean DEBUG = false;
  byte[] rbuf;
  DatagramConnection m_Conn;
  Datagram dg;
  Datagram rdg;

   public byte[] doConnection(String gateway, String URI, String type,int oldTID, String Data) throws IOException
   {

	try
	{

      boolean bReturn = true;

	m_Conn = (DatagramConnection)Connector.open("datagram://"+gateway+":9200",0,true);

	dg = m_Conn.newDatagram(1500);
    rdg = m_Conn.newDatagram(1500);


	int sub = URI.indexOf("http://");

	if (sub == -1)
		{
		 httpsub = "http://";
		 FinalURI = httpsub + URI;
		}
	else
		{
			FinalURI = URI;
		}

	   byte plen = (byte)FinalURI.length();
       byte postMsg[] = FinalURI.getBytes();
	   dg.reset();

	/* Begin New (0.0.5) Code */

	TID++;
	dg.writeByte(TID); /* Transaction ID */

	if (type.equals("GET"))
	   {
		   dg.writeByte(0x40); /* Indicates GET PDU */
		   dg.writeByte(plen);
	   }

	if (type.equals("POST"))
		{
			dg.writeByte(0x60); /* POST PDU */
			dg.writeByte(plen);
	    }

	for (int i = 0; i < postMsg.length; i++)
		    dg.writeByte(postMsg[i]);

    postMsg = null;

		if (type.equals("POST"))
			dg.writeByte(0x92); /* x-form-url-encoded */

		dg.writeByte(0xA9); /* User-Agent */

	  /* j2wap blessing code, i.e. HTTP Headers*/

    String eMsg ="j2wap Version 1.1:See j2wap.com for details";

	byte epostMsg[] = eMsg.getBytes();

	for (int i = 0; i < epostMsg.length; i++)
				   dg.writeByte(epostMsg[i]);

	epostMsg = null;

    dg.writeByte(0x00); /* Terminate Agent String */
	dg.writeByte(0x80); /* Accept */
	dg.writeByte(0x94); /* application/vnd.wap.wmlc */

	/* UTF 8 - Required */

	dg.writeByte(0x81); /* Accept-Charset */
	dg.writeByte(0xEA); /* UTF 8 */

	/* Standard, Latin-1 Charset */

	dg.writeByte(0x81); /* Accept-Charset */
	dg.writeByte(0x84); /* iso-8859-1 */

	/* Add POST Data */

	if (type.equals("POST"))
	{
		byte postData[] = Data.getBytes();
	 	      for (int i = 0; i < postData.length; i++)
	 				{
	 					   dg.writeByte(postData[i]);
	 				}
		postData = null;
	}


	/* Send the Datagram */
		m_Conn.send(dg);

	/* Get rid of the Datagram to save space */
   		dg = null;


    // Now call garbage collection.
    	System.gc();

		m_Conn.receive(rdg);
		byte b[]  = new byte[rdg.getLength()];
		rdg.readFully(b);
		int q = 0;
		byte c[] = new byte[rdg.getLength()];
		int skip;

		    skip = ((int)b[3]);


		    if (skip < 0)
		      {

		      /* Multi-Byte Integer Handling */

			  /* This is required because java doesn't handle unsigned properly */

    		  byte multiPt1 = b[3];
		      byte multiPt2 = b[4];
		      byte finalMult;
		      int tmp1;
		      int tmp2;
		      tmp1  = (int)b[3] + 255;
		      tmp1  += 1;
		      tmp2 = (int)multiPt2;
		      skip = tmp1+tmp2;
		       }

		    skip += 4;
		    System.arraycopy(b,skip,c,0,(b.length-(skip)));

		    /* Code to check for HTTP redirects */

		    int httpstart = 0;
		    int semaphore = 0;
		    int httpend = 0;
		    int count = 0;
		    	int WMLhighpoint = c.length;
			boolean NoWML = true;
			int highpoint = c.length;

			/* Bounds check */

		    if (c.length > 255)
			highpoint = 255;

			if (c.length < 255)
			highpoint = c.length;

            for(count = 0;count < highpoint;count++) {
                if(b[count] == 0xffffff9C) {
                    if(b[count + 1] == 0x68) {
                        if(b[count + 2] == 0x74) {
                            /* We've hit a redirect */
                            httpstart = count;
                            semaphore = 1;
                            break;
                        }
                    }
                }
            }


		for ( count = 0; count < highpoint; count++)
		{
		  if (b[count] == 0xffffff9C)
		  {
		     if (b[count+1] == 0x68)
		     {
		       if(b[count+2] == 0x74)
		       {
		        /* We've hit a redirect */
		        httpstart = count;
		        semaphore = 1;
		        break;
		       }
		     }
		  }
		}


	if (semaphore > 0)
		{

		  for (count = httpstart; count < highpoint; count++)
		  {
		    if (b[count] == 0x00)
		    {
		      if (b[count+1] == 0xffffff9c)
		      {
		      }
		      if (b[count+1] != 0xffffff9C)
		          /* This is the end of the redirect */
		      {
		         httpend = count;
		         break;
		      }

		    }


		}
		int count2;
		String temp;

		  if (httpstart > 0)
		  {
		   byte[] newURL = new byte[httpend-httpstart-1];
		   System.arraycopy(b,httpstart+1,newURL,0,httpend-httpstart-1);

		  String bufURL = new String(newURL);
		  StringBuffer tempURL = new StringBuffer(bufURL);

		  if (semaphore > 0)
		  {

		    for (count2 = 0; count2 < tempURL.length();count2++)
		    	{
		      	if (tempURL.charAt(count2) == 0x00 )
		    			{
		                        tempURL.deleteCharAt(count2);
						}
		        if(tempURL.charAt(count2) == '

⌨️ 快捷键说明

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