用Socket类实现HTTP协议客户端应用 |
<!--*******************************-->梁颖健 Http客户端程序已集成在Java语言中,可以通过URLConnection类调用。遗憾的 ------------------------------------ HTTP/1.1表示这个HTTP服务器是1.1版,200是服务器对客户请求的应答状态码,OK 是对应答状态码的解释,之后是这个文档的元信息和文档正文。(相关应答状态码和元 信息的解释请参阅Inetrnet标准草案:RFC2616)。 3. HTTP客户端程序: import java.net.*; import java.io.*; import java.util.Properties; import java.util.Enumeration; public class Http { protected Socket client; protected BufferedOutputStream sender; protected BufferedInputStream receiver; protected ByteArrayInputStream byteStream; protected URL target; private int responseCode=-1; private String responseMessage=""; private String serverVersion=""; private Properties header = new Properties(); public Http() { } public Http(String url) { GET(url) ; } /* GET方法根据URL,会请求文件、数据库查询结果、程序运行结果等多种内容 */ public void GET(String url) { try { checkHTTP(url); openServer(target.getHost(),target.getPort() ); String cmd = "GET "+ getURLFormat(target) +" HTTP/1.0/r/n" + getBaseHeads()+"/r/n"; sendMessage(cmd); receiveMessage(); }catch(ProtocolException p) { p.printStackTrace(); return; }catch(UnknownHostException e) { e.printStackTrace(); return; }catch(IOException i) i.printStackTrace(); return; } } /* * HEAD方法只请求URL的元信息,不包括URL本身。若怀疑本机和服务器上的 * 文件相同,用这个方法检查最快捷有效。 */ public void HEAD(String url) { try { checkHTTP(url); openServer(target.getHost(),target.getPort() ); String cmd = "HEAD "+getURLFormat(target)+" HTTP/1.0/r/n" +getBaseHeads()+"/r/n"; sendMessage(cmd); receiveMessage(); }catch(ProtocolException p) { p.printStackTrace(); return; }catch(UnknownHostException e) { e.printStackTrace(); return; }catch(IOException i) i.printStackTrace(); return; } } /* * POST方法是向服务器传送数据,以便服务器做出相应的处理。例如网页上常用的 * 提交表格。 */ public void POST(String url,String content) { try { checkHTTP(url); openServer(target.getHost(),target.getPort() ); String cmd = "POST "+ getURLFormat(target) +" HTTP/1.0/r/n"+getBaseHeads(); cmd += "Content-type: application/x-www-form-urlencoded/r/n"; cmd += "Content-length: " + content.length() + "/r/n/r/n"; cmd += content+"/r/n"; sendMessage(cmd); receiveMessage(); }catch(ProtocolException p) { p.printStackTrace(); return; }catch(UnknownHostException e) { e.printStackTrace(); return; }catch(IOException i) i.printStackTrace(); return; } } protected void checkHTTP(String url) throws ProtocolException { try { URL target = new URL(url); if(target==null || !target.getProtocol().toUpperCase().equals("HTTP") ) throw new ProtocolException("这不是HTTP协议"); this.target = target; }catch(MalformedURLException m) { throw new ProtocolException("协议格式错误"); } } /* * 与Web服务器连接。若找不到Web服务器,InetAddress会引发UnknownHostException * 异常。若Socket连接失败,会引发IOException异常。 */ protected void openServer(String host,int port) throws UnknownHostException,IOException { header.clear(); responseMessage=""; responseCode=-1; try { if(client!=null) closeServer(); if(byteStream != null) { byteStream.close(); byteStream=null; } InetAddress address = InetAddress.getByName(host); client = new Socket(address,port==-1?80:port); sender = new BufferedOutputStream(client.getOutputStream()); receiver = new BufferedInputStream(client.getInputStream()); }catch(UnknownHostException u) { throw u; }catch(IOException i) { throw i; } } /* 关闭与Web服务器的连接 */ protected void closeServer() throws IOException { if(client==null) return; try { client.close(); sender.close(); receiver.close(); }catch(IOException i) { throw i; } client=null; sender=null; receiver=null; } protected String getURLFormat(URL target) { String spec = "http://"+target.getHost(); if(target.getPort()!=-1) spec+=":"+target.getPort(); return spec+=target.getFile(); } /* 向Web服务器传送数据 */ protected void sendMessage(String data) throws IOException{ sender.write(data.getBytes(),0,data.length()); sender.flush(); } /* 接收来自Web服务器的数据 */ protected void receiveMessage() throws IOException{ byte data[] = new byte[1024]; int count=0; int word=-1; // 解析第一行 while( (word=receiver.read())!=-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>responseCode = Integer.parseInt(message.substring(mark+1,mark+=4));<br>responseMessage = message.substring(mark,message.length()).trim();<br><br>// 应答状态码和处理请读者添加<br>switch(responseCode) {<br>case 400:<br>throw new IOException("错误请求");<br>case 404:<br>throw new FileNotFoundException( getURLFormat(target) );<br>case 503:<br>throw new IOException("服务器不可用" );<br>}<br>if(word==-1) throw new ProtocolException("信息接收异常终止");<br>int symbol=-1;<br>count=0;<br>// 解析元信息<br>while( word!='/r' && word!='/n' && word>-1) {<br>if(word=='/t') word=32;<br>if(count==data.length) data = addCapacity(data);<br>data[count++] = (byte)word;<br>parseLine: {<br>while( (symbol=receiver.read()) >-1 ) {<br>switch(symbol) {<br>case '/t':<br>symbol=32; break;<br>case '/r':<br>case '/n':<br>word = receiver.read();<br>if( symbol=='/r' && word=='/n') {<br>word=receiver.read();<br>if(word=='/r') word=receiver.read();<br>}<br>if( word=='/r' || word=='/n' || word>32) break parseLine;<br>symbol=32; break;<br>}<br>if(count==data.length) data = addCapacity(data);<br>data[count++] = (byte)symbol;<br>}<br>word=-1;<br>}<br>message = new String(data,0,count);<br>mark = message.indexOf(':');<br>String key = null;<br>if(mark>0) key = message.substring(0,mark);<br>mark++;<br>while( mark<message.length message.charat mark>String value = message.substring(mark,message.length() );<br>header.put(key,value);<br>count=0;<br>}<br>// 获得正文数据<br>while( (word=receiver.read())!=-1) {<br>if(count == data.length) data = addCapacity(data);<br>data[count++] = (byte)word;<br>}<br>if(count>0) byteStream = new ByteArrayInputStream(data,0,count);<br>data=null;<br>closeServer();<br>}<br>public String getResponseMessage() {<br>return responseMessage;<br>}<br>public int getResponseCode() {<br>return responseCode;<br>}<br>public String getServerVersion() {<br>return serverVersion;<br>}<br>public InputStream getInputStream() {<br>return byteStream;<br>}<br>public synchronized String getHeaderKey(int i) {<br>if(i>=header.size()) return null;<br>Enumeration enum = header.propertyNames();<br>String key = null;<br>for(int j=0; jkey = (String)enum.nextElement();<br>return key;<br>}<br>public synchronized String getHeaderValue(int i) {<br>if(i>=header.size()) return null;<br>return header.getProperty(getHeaderKey(i));<br>}<br>public synchronized String getHeaderValue(String key) {<br>return header.getProperty(key);<br>}<br>protected String getBaseHeads() {<br>String inf = "User-Agent: myselfHttp/1.0/r/n"+<br>"Accept: www/source; text/html; image/gif; */*/r/n";<br>return inf;<br>}<br>private byte[] addCapacity(byte rece[]){<br>byte temp[] = new byte[rece.length+1024];<br>System.arraycopy(rece,0,temp,0,rece.length);<br>return temp;<br>}<br>}<br><br>注: 程序中只实现GET、HEAD、POST三种方法。其他几种因不常使用,暂且忽略。</message.length></message.length> |