Http客戶端程序已集成在java語(yǔ)言中,可以通過(guò)URLConnection類調(diào)用。遺憾的
是,由于SUN沒(méi)有公布Http客戶程序的源碼,它實(shí)現(xiàn)的細(xì)節(jié)仍是一個(gè)謎。本文根據(jù)HTTP
協(xié)議規(guī)范,用Java.net.Socket類實(shí)現(xiàn)一個(gè)HTTP協(xié)議客戶端程序。
1.Socket類:
了解TCP/ip協(xié)議集通信的讀者知道,協(xié)議間的通信是通過(guò)Socket完成的。在
Java.net包中,Socket類就是對(duì)Socket的具體實(shí)現(xiàn)。它通過(guò)連接到主機(jī)后,返回一個(gè)
I/O流,實(shí)現(xiàn)協(xié)議間的信息交換。
2.HTTP協(xié)議
HTTP協(xié)議同其它TCP/IP協(xié)議集中的協(xié)議一樣,是遵循客戶/服務(wù)器模型工作的。客
戶端發(fā)往服務(wù)端的信息格式如下:
------------------------------
請(qǐng)求方法URLHTTP協(xié)議的版本號(hào)
提交的元信息
**空行**
實(shí)體
------------------------------
請(qǐng)求方法是對(duì)這次連接工作的說(shuō)明,目前HTTP協(xié)議已經(jīng)發(fā)展到1.1版,它包括GET、
HEAD、POST、DELETE、OPTIONS、TRACE、PUT七種。元信息是關(guān)于當(dāng)前請(qǐng)求的信息。通
過(guò)分析元信息,可以檢查實(shí)體數(shù)據(jù)是否完整,接收過(guò)程是否出錯(cuò),類型是否匹配等。元
信息的引入使HTTP協(xié)議通信更加穩(wěn)妥可靠。實(shí)體是請(qǐng)求的具體內(nèi)容。
將上述報(bào)文發(fā)往Web服務(wù)器,假如成功,應(yīng)答格式如下:
--------------------------------
HTTP協(xié)議的版本號(hào)應(yīng)答狀態(tài)碼應(yīng)答狀態(tài)碼說(shuō)明
接收的元信息
**空行**
實(shí)體
--------------------------------
以上報(bào)文發(fā)向客戶端,并且接收成功,彼此間關(guān)閉連接,完成一次握手。
下面用最常用的GET方法,來(lái)說(shuō)明具體的報(bào)文應(yīng)用
----------------------------------
GEThttp://www.youhost.comHTTP/1.0
accept:www/source;text/Html;image/gif;image/jpeg;*/*
User_Agent:myAgent
**空行**
-----------------------------------
這個(gè)報(bào)文是向www.youhost.com主機(jī)請(qǐng)求一個(gè)缺省HTML文檔。客戶端HTTP協(xié)議版本
號(hào)是1.0版,元信息包括可接收的文件格式,用戶代理,每一段之間用回車換行符分
隔,最后以一個(gè)空行結(jié)束。發(fā)向服務(wù)器后,假如執(zhí)行過(guò)程正常,服務(wù)器返回以下代碼:
------------------------------------
HTTP/1.1200OK
Date:Tue,14Sep199902:19:57GMT
Server:Apache/1.2.6
Connection:close
Content-Type:text/html
**空行**
<html><head>...</head><body>...</body></html>
------------------------------------
HTTP/1.1表示這個(gè)HTTP服務(wù)器是1.1版,200是服務(wù)器對(duì)客戶請(qǐng)求的應(yīng)答狀態(tài)碼,OK
是對(duì)應(yīng)答狀態(tài)碼的解釋,之后是這個(gè)文檔的元信息和文檔正文。(相關(guān)應(yīng)答狀態(tài)碼和元
信息的解釋請(qǐng)參閱Inetrnet標(biāo)準(zhǔn)草案:RFC2616)。
Http.java
importjava.net.*;
importjava.io.*;
importjava.util.PRoperties;
importjava.util.Enumeration;
publicclassHttp{
protectedSocketclient;
protectedBufferedOutputStreamsender;
protectedBufferedInputStreamreceiver;
protectedByteArrayInputStreambyteStream;
protectedURLtarget;
privateintresponseCode=-1;
privateStringresponseMessage="";
privateStringserverVersion="";
privatePropertiesheader=newProperties();
publicHttp(){}
publicHttp(Stringurl){
GET(url);
}
/*GET方法根據(jù)URL,會(huì)請(qǐng)求文件、數(shù)據(jù)庫(kù)查詢結(jié)果、程序運(yùn)行結(jié)果等多種內(nèi)容*/
publicvoidGET(Stringurl){
try{
checkHTTP(url);
openServer(target.getHost(),target.getPort());
Stringcmd="GET"+getURLFormat(target)+"HTTP/1.0/r/n"
+getBaseHeads()+"/r/n";
sendMessage(cmd);
receiveMessage();
}catch(ProtocolExceptionp){
p.printStackTrace();
return;
}catch(UnknownHostExceptione){
e.printStackTrace();
return;
}catch(IOExceptioni){
i.printStackTrace();
return;
}
}
/*
*HEAD方法只請(qǐng)求URL的元信息,不包括URL本身。若懷疑本機(jī)和服務(wù)器上的
*文件相同,用這個(gè)方法檢查最快捷有效。
*/
publicvoidHEAD(Stringurl){
try{
checkHTTP(url);
openServer(target.getHost(),target.getPort());
Stringcmd="HEAD"+getURLFormat(target)+"HTTP/1.0/r/n"
+getBaseHeads()+"/r/n";
sendMessage(cmd);
receiveMessage();
}catch(ProtocolExceptionp){
p.printStackTrace();
return;
}catch(UnknownHostExceptione){
e.printStackTrace();
return;
}catch(IOExceptioni){
i.printStackTrace();
return;
}
}
/*
*POST方法是向服務(wù)器傳送數(shù)據(jù),以便服務(wù)器做出相應(yīng)的處理。例如網(wǎng)頁(yè)上常用的
*提交表格。
*/
publicvoidPOST(Stringurl,Stringcontent){
try{
checkHTTP(url);
openServer(target.getHost(),target.getPort());
Stringcmd="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(ProtocolExceptionp){
p.printStackTrace();
return;
}catch(UnknownHostExceptione){
e.printStackTrace();
return;
}catch(IOExceptioni){
i.printStackTrace();
return;
}
}
protectedvoidcheckHTTP(Stringurl)throwsProtocolException{
try{
URLtarget=newURL(url);
if(target==null!target.getProtocol().toUpperCase().equals("HTTP"))
thrownewProtocolException("這不是HTTP協(xié)議");
this.target=target;
}catch(MalformedURLExceptionm){
thrownewProtocolException("協(xié)議格式錯(cuò)誤");
}
}
/*
*與Web服務(wù)器連接。若找不到Web服務(wù)器,InetAddress會(huì)引發(fā)UnknownHostException
*異常。若Socket連接失敗,會(huì)引發(fā)IOException異常。
*/
protectedvoidopenServer(StrinGhost,intport)throws
UnknownHostException,IOException{
header.clear();
responseMessage="";responseCode=-1;
try{
if(client!=null)closeServer();
if(byteStream!=null){
byteStream.close();byteStream=null;
}
InetAddressaddress=InetAddress.getByName(host);
client=newSocket(address,port==-1?80:port);
sender=newBufferedOutputStream(client.getOutputStream());
receiver=newBufferedInputStream(client.getInputStream());
}catch(UnknownHostExceptionu){
throwu;
}catch(IOExceptioni){
throwi;
}
}
/*關(guān)閉與Web服務(wù)器的連接*/
protectedvoidcloseServer()throwsIOException{
if(client==null)return;
try{
client.close();sender.close();receiver.close();
}catch(IOExceptioni){
throwi;
}
client=null;sender=null;receiver=null;
}
protectedStringgetURLFormat(URLtarget){
Stringspec="http://"+target.getHost();
if(target.getPort()!=-1)
spec+=":"+target.getPort();
returnspec+=target.getFile();
}
/*向Web服務(wù)器傳送數(shù)據(jù)*/
protectedvoidsendMessage(Stringdata)throwsIOException{
sender.write(data.getBytes(),0,data.length());
sender.flush();
}
/*接收來(lái)自Web服務(wù)器的數(shù)據(jù)*/
protectedvoidreceiveMessage()throwsIOException{
bytedata[]=newbyte[1024];
intcount=0;
intWord=-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;
}
Stringmessage=newString(data,0,count);
intmark=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();
//應(yīng)答狀態(tài)碼和處理請(qǐng)讀者添加
switch(responseCode){
case400:
thrownewIOException("錯(cuò)誤請(qǐng)求");
case404:
thrownewFileNotFoundException(getURLFormat(target));
case503:
thrownewIOException("服務(wù)器不可用");
}
if(word==-1)thrownewProtocolException("信息接收異常終止");
intsymbol=-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)breakparseLine;
symbol=32;break;
}
if(count==data.length)data=addCapacity(data);
data[count++]=(byte)symbol;
}
word=-1;
}
message=newString(data,0,count);
mark=message.indexOf(':');
Stringkey=null;
if(mark>0)key=message.substring(0,mark);
mark++;
while(mark<message.length()&&message.charAt(mark)<=32)mark++;
Stringvalue=message.substring(mark,message.length());
header.put(key,value);
count=0;
}
//獲得正文數(shù)據(jù)
while((word=receiver.read())!=-1){
if(count==data.length)data=addCapacity(data);
data[count++]=(byte)word;
}
if(count>0)byteStream=newByteArrayInputStream(data,0,count);
data=null;
closeServer();
}
publicStringgetResponseMessage(){
returnresponseMessage;
}
publicintgetResponseCode(){
returnresponseCode;
}
publicStringgetServerVersion(){
returnserverVersion;
}
publicInputStreamgetInputStream(){
returnbyteStream;
}
publicsynchronizedStringgetHeaderKey(inti){
if(i>=header.size())returnnull;
Enumerationenum=header.propertyNames();
Stringkey=null;
for(intj=0;j<=i;j++)
key=(String)enum.nextElement();
returnkey;
}
publicsynchronizedStringgetHeaderValue(inti){
if(i>=header.size())returnnull;
returnheader.getProperty(getHeaderKey(i));
}
publicsynchronizedStringgetHeaderValue(Stringkey){
returnheader.getProperty(key);
}
protectedStringgetBaseHeads(){
Stringinf="User-Agent:myselfHttp/1.0/r/n"+
"Accept:www/source;text/html;image/gif;*/*/r/n";
returninf;
}
privatebyte[]addCapacity(byterece[]){
bytetemp[]=newbyte[rece.length+1024];
System.arraycopy(rece,0,temp,0,rece.length);
returntemp;
}
publicstaticvoidmain(String[]args){
Httphttp=newHttp();
//http.GET("http://192.168.1.5");
inti;
for(i=0;i<50000;i++){
http.GET("http://www.model-dl.com/modelinfo.asp?modelid=101");
http.POST("http://www.model-dl.com/modelinfo.asp?modelid=101","ratecontd=101&MM_insert=form1");
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注