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

📄 httprequest.java

📁 使用java语言编写的小应用程序
💻 JAVA
字号:
package http;
import java.io.*;
import java.net.*;
import java.util.Vector;
import javax.servlet.ServletInputStream;


public class HttpRequest{

public  String HttpMethomd;
  String Requesturl;
  String RequestSouc;
  String HttpVersion;
  String GetRequest;
  int statuscode=200;
  Vector ReadFromClient=null;
  
  String HttpQuery=null;
  Socket Client;

  BufferedReader receivmsg;
    
 HttpRequest(Socket Client){
 	this.Client=Client;
 	readFromClient();
 	
  	
//  	HttpQuery=(String)ReadFromClient.firstElement();
  	
  	ConstrueRequest();  	 
  }//end HttpRequest()
  
  void ConstrueRequest(){
  	
    try{
       RequestSouc=HttpQuery;
       
       int indexpre,indexnex;
       if (HttpQuery==""){
       	  statuscode=404;
       	  return;}
       	  
       indexpre=HttpQuery.indexOf(' ');
       HttpMethomd=HttpQuery.substring(0,indexpre);
       indexpre++;
       indexnex=HttpQuery.indexOf(' ',indexpre);
       
       Requesturl = HttpQuery.substring(indexpre,indexnex);
       


       if ( Requesturl.lastIndexOf('?') != -1 ){
       	       RequestSouc=Requesturl.substring( 0,Requesturl.lastIndexOf('?') );
       	       GetRequest=Requesturl.substring( Requesturl.lastIndexOf('?')+1,Requesturl.length() );
       }else{
           GetRequest="";
           RequestSouc=Requesturl;
       	}

       //System.out.println("RequestSouc "+RequestSouc);
       //System.out.println("GetRequest "+GetRequest);
	  
       HttpMethomd.toUpperCase();
       
    }catch (StringIndexOutOfBoundsException e){
    	HttpMethomd="GET";
      	statuscode=500;
      	System.out.println("err1");
    	return;}
    catch (NullPointerException e){
    	HttpMethomd="GET";
    	statuscode=500;
    	System.out.println("err2");
    	return;}
     
  }//end ConstrueRequest()  

//synchronized
 
void readFromClient(){
    ReadFromClient = new Vector();
    try{
       receivmsg =new BufferedReader(new InputStreamReader(Client.getInputStream()));
              
      int c=0,off=0;
      char[] readbytes=new char[1024*1024];
   
//      ReadFromClient.addElement();
      HttpQuery=receivmsg.readLine();

      while(receivmsg.ready()){
	 c = receivmsg.read();
	 //System.out.print((char)c);
	 
	 
	 if (c == '\n'){
	    ReadFromClient.addElement(new String(readbytes,0,off-1));
       	    off=0;
	    continue;
	 }	
	 readbytes[off++]=(char)c;
      }

    }catch (IOException e) {
	CloseConnect();
    }//end catch

}


void CloseConnect(){
   try{
	receivmsg.close();
	Client.close();
   }catch (IOException e) {}//end catch
}


}//end class HttpRequest

⌨️ 快捷键说明

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