快速业务通道

用Socket类实现HTTP协议客户端应用 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-23
1 ) { if(word==''\r''||word==''\n'') { word=receiver.read(); if(word==''\n'') word=receiver.read(); break; } if(count == data.length) data = addCapacity(data); data[count++]=(byte)word; } String message = new String(data,0,count); int mark = message.indexOf(32); serverVersion = message.substring(0,mark); while( mark<message.length() && message.charAt(mark+1)==32 ) mark++; responseCode = Integer.parseInt(message.substring(mark+1,mark+=4)); responseMessage = message.substring(mark,message.length()).trim(); // 应答状态码和处理请读者添加 switch(responseCode) { case 400: throw new IOException("错误请求"); case 404: throw new FileNotFoundException( getURLFormat(target) ); case 503: throw new IOException("服务器不可用" ); } if(word==-1) throw new ProtocolException("信息接收异常终止"); int symbol=-1; count=0; // 解析元信息 while( word!=''\r'' && word!=''\n'' && word>-1) { if(word==''\t'') word=32; if(count==data.length) data = addCapacity(data); data[count++] = (byte)word; parseLine: { while( (symbol=receiver.read()) >-1 ) { switch(symbol) { case ''\t'': symbol=32; break; case ''\r'': case ''\n'': word = receiver.read(); if( symbol==''\r'' && word==''\n'') { word=receiver.read(); if(word==''\r'') word=receiver.read(); } if( word==''\r'' || word==''\n'' || word>32) break parseLine; symbol=32; break; } if(count==data.length) data = addCapacity(data); data[count++] = (byte)symbol; } word=-1; } message = new String(data,0,count); mark = message.indexOf('':''); String key = null; if(mark>0) key = message.substring(0,mark); mark++; while( mark<message.length() && message.charAt(mark)<=32 ) mark++; String value = message.substring(mark,message.length() ); header.put(key,value); count=0; } // 获得正文数据 while( (word=receiver.read())!=-1) { if(count == data.length) data = addCapacity(data); data[count++] = (byte)word; } if(count>0) byteStream = new ByteArrayInputStream(data,0,count); data=null; closeServer(); } public String getResponseMessage() { return responseMessage; } public int getResponseCode() { return responseCode; } public String getServerVersion() { return serverVersion; } public InputStream getInputStream() { return byteStream; } public synchronized String getHeaderKey(int i) { if(i>=header.size()) return null; Enumeration enum = header.propertyNames(); String key = null; for(int j=0; j<=i; j++) key = (String)enum.nextElement(); return key; } public synchronized String getHeaderValue(int i) { if(i>=header.size()) return null; return header.getProperty(getHeaderKey(i)); } public synchronized String getHeaderValue(String key) { return header.getProperty(key); } protected String getBaseHeads() { String inf = "User-Agent: myselfHttp/1.0\r\n"+ "Accept: www/source; text/html; image/gif; */*\r\n"; return inf; } private byte[] addCapacity(byte rece[]){ byte temp[] = new byte[rece.length+1024]; System.arraycopy(rece,0,temp,0,rece.length)

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号