HttpClient with java Socket possible to achieve a mistake

  A java Socket HttpClient possible to achieve a mistake 

  Import java.io.ByteArrayOutputStream; 
  Import java.io.IOException; 
  Import java.net.Socket; 
  Import java.net.UnknownHostException; 

  (Public class test2 
  Public static void main (String arg []) throws Exception 
  ( 
  Try ( 
  Socket socket = new Socket ( "www.baidu.com," 80); 
  Byte [] bytes = ( "GET / HTTP/1.1 \ n" + 
  "Host: www.baidu.com \ n" + 
  "Connection: close \ n" + 
  "User-agent: Mozilla/4.0 \ n" + 
  "Accept-language: zh-cn \ n" + 
  "\ N"). GetBytes ( "UTF-8"); 
  Socket.getOutputStream (). Write (bytes); 
  ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream (8192); 

  Int c; 
  While ((c = socket.getInputStream (). Read ())! = -1) ( 
  ArrayOutputStream.write (c); 
  ) 

  System.out.println (new String (arrayOutputStream.toByteArray ())); 

  ) Catch (UnknownHostException e) ( 
  E.printStackTrace (); 

  ) Catch (IOException e) ( 
  E.printStackTrace (); 
  ) 

  ) 
  ) 

  Note that if written GET Get some sites may not have the http documents, such as www.baidu.com 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , ,

Releated Java Articles

Comments

Leave a Reply