欢迎来到天天文库
浏览记录
ID:41006305
大小:35.00 KB
页数:6页
时间:2019-08-13
《使用java控制HTTP协议客户端应用》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、importjava.net.*;importjava.io.*;importjava.util.Properties;importjava.util.Enumeration;publicclassHttp{protectedSocketclient;protectedBufferedOutputStreamsender;protectedBufferedInputStreamreceiver;protectedByteArrayInputStreambyteStream;protectedURLtarget;privateintresponseCod
2、e=-1;privateStringresponseMessage="";privateStringserverVersion="";privatePropertiesheader=newProperties();publicHttp(){}publicHttp(Stringurl){GET(url);}/*GET方法根据URL,会请求文件、数据库查询结果、程序运行结果等多种内容*/publicvoidGET(Stringurl){try{checkHTTP(url);openServer(target.getHost(),target.getPort
3、());Stringcmd="GET"+getURLFormat(target)+"HTTP/1.0r"+getBaseHeads()+"r";sendMessage(cmd);receiveMessage();}catch(ProtocolExceptionp){p.printStackTrace();return;}catch(UnknownHostExceptione){e.printStackTrace();return;}catch(IOExceptioni)i.printStackTrace();return;}}/**HEAD
4、方法只请求URL的元信息,不包括URL本身。若怀疑本机和服务器上的*文件相同,用这个方法检查最快捷有效。*/publicvoidHEAD(Stringurl){try{checkHTTP(url);openServer(target.getHost(),target.getPort());Stringcmd="HEAD"+getURLFormat(target)+"HTTP/1.0r"+getBaseHeads()+"r";sendMessage(cmd);receiveMessage();}catch(ProtocolExceptionp
5、){p.printStackTrace();return;}catch(UnknownHostExceptione){e.printStackTrace();return;}catch(IOExceptioni)i.printStackTrace();return;}}/**POST方法是向服务器传送数据,以便服务器做出相应的处理。例如网页上常用的*提交表格。*/publicvoidPOST(Stringurl,Stringcontent){try{checkHTTP(url);openServer(target.getHost(),target.ge
6、tPort());Stringcmd="POST"+getURLFormat(target)+"HTTP/1.0r"+getBaseHeads();cmd+="Content-type:application/x-www-form-urlencodedr";cmd+="Content-length:"+content.length()+"rr";cmd+=content+"r";sendMessage(cmd);receiveMessage();}catch(ProtocolExceptionp){p.printStack
7、Trace();return;}catch(UnknownHostExceptione){e.printStackTrace();return;}catch(IOExceptioni)i.printStackTrace();return;}}protectedvoidcheckHTTP(Stringurl)throwsProtocolException{try{URLtarget=newURL(url);if(target==null
8、
9、!target.getProtocol().toUpperCase().equals("HTTP"))throwne
10、wProtocolException("这不是HTTP协议");this.target=tar
此文档下载收益归作者所有