免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

JavaSocket應答怎么實現

本篇內容主要講解“Java Socket應答怎么實現”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java Socket應答怎么實現”吧!

成都創新互聯公司-專業網站定制、快速模板網站建設、高性價比龍井網站開發、企業建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式龍井網站制作公司更省心,省錢,快速模板網站建設找我們,業務覆蓋龍井地區。費用合理售后完善,10年實體公司更值得信賴。

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

Http.java   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+1)==32 ) mark++;   responseCode = Integer.parseInt(message.substring(mark+1,mark+=4));   responseMessage = message.substring(mark,message.length()).trim();   // 應答狀態碼和處理請讀者添加   switch(responseCode) {   case 400:   throw new IOException("錯誤請求");   case 404:   throw new FileNotFoundException( getURLFormat(target) );   case 503:   throw new IOException("服務器不可用" );   }   if(word==-1) throw new ProtocolException("信息接收異常終止");   int symbol=-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) break parseLine;   symbol=32; break;   }   if(count==data.length) data = addCapacity(data);   data[count++] = (byte)symbol;   }   word=-1;   }   message = new String(data,0,count);   mark = message.indexOf(':');   String key = null;   if(mark>0) key = message.substring(0,mark);   mark++;   while( mark<message.length() && message.charAt(mark)<=32 ) mark++;   String value = message.substring(mark,message.length() );   header.put(key,value);   count=0;   }   // 獲得正文數據   while( (word=receiver.read())!=-1) {   if(count == data.length) data = addCapacity(data);   data[count++] = (byte)word;   }   if(count>0) byteStream = new ByteArrayInputStream(data,0,count);   data=null;   closeServer();   }   public String getResponseMessage() {   return responseMessage;   }   public int getResponseCode() {   return responseCode;   }   public String getServerVersion() {   return serverVersion;   }   public InputStream getInputStream() {   return byteStream;   }   public synchronized String getHeaderKey(int i) {   if(i>=header.size()) return null;   Enumeration enum = header.propertyNames();   String key = null;   for(int j=0; j<=i; j++)   key = (String)enum.nextElement();   return key;   }   public synchronized String getHeaderValue(int i) {   if(i>=header.size()) return null;   return header.getProperty(getHeaderKey(i));   }   public synchronized String getHeaderValue(String key) {   return header.getProperty(key);   }   protected String getBaseHeads() {   String inf = "User-Agent: myselfHttp/1.0\r\n"+   "Accept: www/source; text/html; image/gif; */*\r\n";   return inf;   }   private byte[] addCapacity(byte rece[]){   byte temp[] = new byte[rece.length+1024];   System.arraycopy(rece,0,temp,0,rece.length);   return temp;   }   public static void main(String[] args) {   Http http=new Http();   //http.GET("http://192.168.1.5   );   int i;   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 ");   }   }   }

到此,相信大家對“Java Socket應答怎么實現”有了更深的了解,不妨來實際操作一番吧!這里是創新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

分享題目:JavaSocket應答怎么實現
網頁地址:http://m.newbst.com/article38/jesisp.html

成都網站建設公司_創新互聯,為您提供網頁設計公司關鍵詞優化企業網站制作品牌網站設計網站策劃定制開發

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都app開發公司