當前位置:學者齋 >

IT認證 >SUN認證 >

Java Socket應答與HTTP服務器的關係

Java Socket應答與HTTP服務器的關係

Java Socket應答一直伴隨着我們的編程生活,在不斷的發展中有很多知識需要我們學習。下面我們就先來看看有關Java Socket應答的代碼,有點長,但是看下去就會讓你豁然開朗。

Java Socket應答與HTTP服務器的關係

HTTP/1.1表示這個HTTP服務器是1.1版,200是服務器對客户請求的應答狀態碼,OK是對應答狀態碼的解釋,之後是這個文檔的元信息和文檔正文。(相關應答狀態碼和元信息的解釋請參閲Inetrnet標準草案:RFC2616)。

rt .*;

rt .*;

rt erties;

rt eration;

ic class Http {

ected Socket client;

ected BufferedOutputStream sender;

ected BufferedInputStream receiver;

ected ByteArrayInputStream byteStream;

ected URL target;

ate int responseCode=-1;

ate String responseMessage="";

ate String serverVersion="";

ate Properties header = new Properties();

ic Http() { }

ic Http(String url) {

(url) ;

19.}

20./* GET方法根據URL,會請求文件、數據庫查詢結果、程序運行結果等多種內容 */

ic void GET(String url) {

{

kHTTP(url);

Server(ost(),ort() );

ng cmd = "GET "+ getURLFormat(target) +" HTTP/1.0rn"

26.+ getBaseHeads()+"rn";

Message(cmd);

iveMessage();

29.} catch(ProtocolException p) {

tStackTrace();

rn;

32.} catch(UnknownHostException e) {

tStackTrace();

rn;

35.} catch(IOException i) {

tStackTrace();

rn;

38.}

39.}

40./*

41.* HEAD方法只請求URL的元信息,不包括URL本身。若懷疑本機和服務器上的.

42.* 文件相同,用這個方法檢查最快捷有效。

43.*/

ic void HEAD(String url) {

{

kHTTP(url);

Server(ost(),ort() );

ng cmd = "HEAD "+getURLFormat(target)+" HTTP/1.0rn"

49.+getBaseHeads()+"rn";

Message(cmd);

iveMessage();

52.}catch(ProtocolException p) {

tStackTrace();

rn;

55.}catch(UnknownHostException e) {

tStackTrace();

rn;

58.}catch(IOException i) {

tStackTrace();

rn;

61.}

62.}

63./*

64.* POST方法是向服務器傳送數據,以便服務器做出相應的處理。例如網頁上常用的

65.* 提交表格。

66.*/

ic void POST(String url,String content) {

{

kHTTP(url);

Server(ost(),ort() );

ng cmd = "POST "+ getURLFormat(target) +"HTTP/1.0rn"+getBaseHeads();

+= "Content-type: application/x-www-form-urlencodedrn";

+= "Content-length: " + th() + "rnrn";

+= content+"rn";

Message(cmd);

iveMessage();

77.}catch(ProtocolException p) {

tStackTrace();

rn;

80.}catch(UnknownHostException e) {

tStackTrace();

rn;

83.}catch(IOException i) {

tStackTrace();

rn;

86.}

87.}

ected void checkHTTP(String url) throws ProtocolException {

{

target = new URL(url);

(target==null || !rotocol()perCase()ls("HTTP") )

w new ProtocolException("這不是HTTP協議");

et = target;

94.} catch(MalformedURLException m) {

w new ProtocolException("協議格式錯誤");

96.}

97.}

98./*

99.* 與Web服務器連接。若找不到Web服務器,InetAddress會引發UnknownHostException

100.* 異常。若Socket連接失敗,會引發IOException異常。

101.*/

ected void openServer(String host,int port) throws

ownHostException,IOException {

r();

onseMessage=""; responseCode=-1;

{

(client!=null) closeServer();

(byteStream != null) {

e(); byteStream=null;

110.}

Address address = yName(host);

nt = new Socket(address,port==-1?80:port);

er = new BufferedOutputStream(utputStream());

iver = new BufferedInputStream(nputStream());

115.}catch(UnknownHostException u) {

w u;

117.}catch(IOException i) {

w i;

119.}

120.}

121./* 關閉與Web服務器的連接 */

ected void closeServer() throws IOException {

(client==null) return;

{

e(); e(); e();

126.} catch(IOException i) {

w i;

128.}

nt=null; sender=null; receiver=null;

130.}

ected String getURLFormat(URL target) {

ng spec = "http://

133.+ost();

(ort()!=-1)

+=":"+ort();

rn spec+=ile();

137.}

138./* 向Web服務器傳送數據 */

ected void sendMessage(String data) throws IOException{

e(ytes(),0,th());

h();

142.}

143./* 接收來自Web服務器的數據 */

ected void receiveMessage() throws IOException{

data[] = new byte[1024];

count=0;

word=-1;

148.// 解析第一行

e( (word=())!=-1 ) {

(word=='r'||word=='n') {

=();

(word=='n') word=();

k;

154.}

(count == th) data = addCapacity(data);

[count++]=(byte)word;

157.}

ng message = new String(data,0,count);

mark = xOf(32);

erVersion = tring(0,mark);

e( mark

onseCode = eInt(tring(mark+1,mark+=4));

onseMessage = tring(mark,th())();

164.// 應答狀態碼和處理請讀者添加

ch(responseCode) {

400:

w new IOException("錯誤請求");

404:

w new FileNotFoundException( getURLFormat(target) );

503:

w new IOException("服務器不可用" );

172.}

(word==-1) throw new ProtocolException("信息接收異常終止");

symbol=-1;

t=0;

176.// 解析元信息

e( word!='r' && word!='n' && word>-1) {

(word=='t') word=32;

(count==th) data = addCapacity(data);

[count++] = (byte)word;

eLine: {

e( (symbol=()) >-1 ) {

ch(symbol) {

't':

ol=32; break;

'r':

'n':

= ();

( symbol=='r' && word=='n') {

=();

(word=='r') word=();

192.}

( word=='r' || word=='n' || word>32) break parseLine;

ol=32; break;

195.}

(count==th) data = addCapacity(data);

[count++] = (byte)symbol;

198.}

=-1;

200.}

age = new String(data,0,count);

= xOf(':');

ng key = null;

(mark>0) key = tring(0,mark);

++;

e( mark

ng value = tring(mark,th() );

(key,value);

t=0;

210.}

211.// 獲得正文數據

e( (word=())!=-1) {

(count == th) data = addCapacity(data);

[count++] = (byte)word;

215.}

(count>0) byteStream = new ByteArrayInputStream(data,0,count);

=null;

eServer();

219.}

ic String getResponseMessage() {

rn responseMessage;

222.}

ic int getResponseCode() {

rn responseCode;

225.}

ic String getServerVersion() {

rn serverVersion;

228.}

ic InputStream getInputStream() {

rn byteStream;

231.}

ic synchronized String getHeaderKey(int i) {

(i>=()) return null;

eration enum = ertyNames();

ng key = null;

(int j=0; j<=i; j++)

= (String)Element();

rn key;

239.}

ic synchronized String getHeaderValue(int i) {

(i>=()) return null;

rn roperty(getHeaderKey(i));

243.}

ic synchronized String getHeaderValue(String key) {

rn roperty(key);

246.}

ected String getBaseHeads() {

ng inf = "User-Agent: myselfHttp/1.0rn"+

249."Accept: www/source; text/html; image/gif; */*rn";

rn inf;

251.}

ate byte[] addCapacity(byte rece[]){

temp[] = new byte[th+1024];

ycopy(rece,0,temp,0,th);

rn temp;

256.}

ic static void main(String[] args) {

http=new Http();

259.//("

260.);

i;

(i=0; i<50000; i++) {

(" );

(","ratecontd=101&MM_insert=form1 ");

265.}

266.}

267.}

  • 文章版權屬於文章作者所有,轉載請註明 https://xuezhezhai.com/zh-hk/itrz/sun/lw5rp6.html