资源预览内容
第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Java Socket应答与HTTP服务器的关系 Java Socket应答一直伴随着我们的编程生活,在不断的发展中有很多知识需要我们学习。下面我们就先来看看有关Java Socket应答的代码,有点长,但是看下去就会让你豁然开朗。 HTTP/1.1表示这个HTTP服务器是1.1版,200是服务器对客户请求的应答状态码,OK是对应答状态码的解释,之后是这个文档的元信息和文档正文。(相关应答状态码和元信息的解释请参阅Inetrnet标准草案:RFC2616)。1.Http.java2.import .*;3.import java.io.*;4.import java.util.Properties;5.import java.util.Enumeration;6.public class Http 7.protected Socket client;8.protected BufferedOutputStream sender;9.protected BufferedInputStream receiver;10.protected ByteArrayInputStream byteStream;11.protected URL target;12.private int responseCode=-1;13.private String responseMessage=;14.private String serverVersion=;15.private Properties header = new Properties();16.public Http() 17.public Http(String url) 18.GET(url) ;19.20./* GET方法根据URL,会请求文件、数据库查询结果、程序运行结果等多种内容 */21.public void GET(String url) 22.try 23.checkHTTP(url);24.openServer(target.getHost(),target.getPort() );25.String cmd = GET + getURLFormat(target) + HTTP/1.0rn26.+ getBaseHeads()+rn;27.sendMessage(cmd);28.receiveMessage();29. catch(ProtocolException p) 30.p.printStackTrace();31.return;32. catch(UnknownHostException e) 33.e.printStackTrace();34.return;35. catch(IOException i) 36.i.printStackTrace();37.return;38.39.40./*41.* HEAD方法只请求URL的元信息,不包括URL本身。若怀疑本机和服务器上的42.* 文件相同,用这个方法检查最快捷有效。43.*/44.public void HEAD(String url) 45.try 46.checkHTTP(url);47.openServer(target.getHost(),target.getPort() );48.String cmd = HEAD +getURLFormat(target)+ HTTP/1.0rn49.+getBaseHeads()+rn;50.sendMessage(cmd);51.receiveMessage();52.catch(ProtocolException p) 53.p.printStackTrace();54.return;55.catch(UnknownHostException e) 56.e.printStackTrace();57.return;58.catch(IOException i) 59.i.printStackTrace();60.return;61.62.63./*64.* POST方法是向服务器传送数据,以便服务器做出相应的处理。例如网页上常用的65.* 提交表格。66.*/67.public void POST(String url,String content) 68.try 69.checkHTTP(url);70.openServer(target.getHost(),target.getPort() );71.String cmd = POST + getURLFormat(target) +HTTP/1.0rn+getBaseHeads();72.cmd += Content-type: application/x-www-form-urlencodedrn;73.cmd += Content-length: + content.length() + rnrn;74.cmd += content+rn;75.sendMessage(cmd);76.receiveMessage();77.catch(ProtocolException p) 78.p.printStackTrace();79.return;80.catch(UnknownHostException e) 81.e.printStackTrace();82.return;83.catch(IOException i) 84.i.printStackTrace();85.return;86.87.88.protected void checkHTTP(String url) throws ProtocolException 89.try 90.URL target = new URL(url);91.if(target=null | !target.getProtocol().toUpperCase().equals(HTTP) )92.throw new ProtocolException(这不是HTTP协议);93.this.target = target;94. catch(MalformedURLException m) 95.throw new ProtocolException(协议格式错误);96.97.98./*99.* 与Web服务器连接。若找不到Web服务器,InetAddress会引发UnknownHostException100.* 异常。若Socket连接失败,会引发IOException异常。101.*/102.protected void openServer(String host,int port) throws103.UnknownHostException,IOException 104.header.clear();105.responseMessage=; responseCode=-1;106.try 107.if(client!=null) closeServer();108.if(byteStream != null) 109.byteStream.close(); byteStream=null;110.111.InetAddress address = InetAddress.getByName(host);112.client = new Socket(address,port=-1?80:port);113.sender = new BufferedOutputStream(client.getOutputStream();114.receiver = new BufferedInputStream(client.getInputStream();115.catch(UnknownHostException u) 116.throw u;117.catch(IOException i) 118.throw i;119.120.121./* 关闭与Web服务器的连接 */122.protected void closeServer() throws IOException 123.if(client=null) return;124.try 125.client.close(); sender.close(); receiver.close();126. catch(IOException i) 127.throw i;128.129.client=null; sender=null; receiver=null;130.131.protected String getURLFormat(URL target) 132.String spec = http:/133.+target.getHost();134.if(target.getPort()!=-1)135.spec+=:+target.getPort();136.return spec+=target.getFile();137.138./* 向Web服务器传送数据 */139.protected void sendMessage(String data) throws IOException140.sender.write(data.getBytes(),0,data.length();141.sender.flush();142.143./* 接收来自Web服务器的数据 */144.protected void receiveMessage() throws IOException145.byte data = new byte1024;146.int count=0;147.int word=-1;148./ 解析第一行149.while( (word=receiver.read()!=-1 ) 150.if(word=r|word=n) 151.word=receiver.read();152.if(word=n) word=receiver.read();1
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号