public SocketRequest(InputStream input) { this.input = input; } public SocketRequest(Socket socket) { this.input = socket.getInputStream(); } public void ReadData() { //解析 获得InputStream的数据 ReadHeader(); //头部 if(beChucked) //为Chucked { int ChuckSize=0; while((ChuckSize=getChuckSize())>0) //多个Chucked { readLenData(ChuckSize+2);//读取定长数据 } readLenData(2); //最后的2位 } if(CONTENT_LENGTH>0) { readLenData(CONTENT_LENGTH);//读取定长数据 } uri = "";//parseUri(new String(request)); } private void readLenData(int size) //读取定长数据 { int readed=0; //已经读取数 try{ int available=0;//input.available(); //可读数 if(available>(size-readed)) available=size-readed; while( readed<size ) { while(available==0){ //等到有数据可读 available = input.available(); //可读数 } if(available>(size-readed)) available= size-readed; //size-readed--剩余数 if(available>2048) available= 2048; //size-readed--剩余数 byte[] buffer = new byte[available]; int reading = input.read(buffer); request=request.append(new String(buffer,0,reading)); //byte数组相加 readed+=reading; //已读字符 } }catch(IOException e){ System.out.println("Read readLenData Error!"); } } private void ReadHeader() //读取头部 并获得大小 { byte[] crlf = new byte[1]; int crlfNum= 0; //已经连接的回车换行数 crlfNum=4为头部结束 try{ while( input.read(crlf)!=-1 ) //读取头部 { if(crlf[0]==crlf13 || crlf[0]==crlf10) { crlfNum++; } else { crlfNum=0; } //不是则清 request=request.append(new String(crlf,0,1)); //byte数组相加 if(crlfNum==4) break; } }catch(IOException e){ System.out.println("Read Http Header Error!"); return; } String tempStr=(new String(request)).toUpperCase(); //这里我只处理了GET与POST方法 String strMethod = tempStr.substring(0,4); if(strMethod.equals("GET ")) //前 { beGet=true; } else if(strMethod.equals("POST")) { bePost=true; getContentlen_Chucked(tempStr); } else { System.out.println("不支持的HTTP包类型"); } //其它的其它类型 暂不支持 } |