编程语言

img caoxu1987728

HTTPSocket类和SocketFactory类的实现

发表于2008/10/3 15:40:00  3045人阅读

分类: All Spiders

 HTTPSocket类、SocketFactory:

HTTPSocket类继承了HTTP类中lowLevelSend的抽象方法。

下面简单分析一下lowLevelSend方法:

1,如何建立连接(通过URL)

注:如果要求HTTPS连接,则使用SSL打开连接。

2,确定请求是“GET”还是“POST”,处理所有的客户端标题。

3,读完标题之后,对它们进行解析(必须),然后再读入真正的正文。

代码清单:

  1. package com.heaton.bot;
  2. import java.net.*;
  3. import java.io.*;
  4. public class HTTPSocket extends HTTP {
  5.         
  6.     synchronized public void lowLevelSend(String url,String post)
  7.     throws java.net.UnknownHostException, java.io.IOException {
  8.         String command;// What kind of send POST or GET
  9.         StringBuffer headers;// Used to hold outgoing client headers
  10.         byte buffer[]=new byte[1024];//A buffer for reading
  11.         int l,i;// Counters
  12.         Attribute a;// Used to process incoming attributes
  13.         int port=80;// What port, default to 80
  14.         boolean https = false;// Are we using HTTPS?
  15.         URL u;// Used to help parse the url parameter
  16.         Socket socket = null;
  17.         OutputStream out = null;
  18.         InputStream in = null;
  19.         
  20.         // parse the URL
  21.         try {
  22.             if ( url.toLowerCase().startsWith("https") ) {
  23.                 url = "http" + url.substring(5);// so it can be parsed
  24.                 u = new URL(url);
  25.                 if ( u.getPort()==-1 )
  26.                     port=443;
  27.                 https = true;
  28.             } else
  29.                 u = new URL(url);
  30.             
  31.             if ( u.getPort()!=-1 )
  32.                 port = u.getPort();
  33.             
  34.             // connect
  35.             socket = SocketFactory.getSocket(u.getHost(),port,https);
  36.             
  37.             socket.setSoTimeout(timeout);
  38.             out = socket.getOutputStream();
  39.             in = socket.getInputStream();
  40.             
  41.             // send command, i.e. GET or POST
  42.             if ( post == null )
  43.                 command = "GET ";
  44.             else
  45.                 command = "POST ";
  46.             
  47.             String file = u.getFile();
  48.             if ( file.length()==0 )
  49.                 file="/";
  50.             
  51.             // proxy support
  52.             
  53.             if( SocketFactory.useProxy()) {
  54.                 addProxyAuthHeader();
  55.                 if(port!=80)
  56.                     file = "http://" + u.getHost() + ":" + port + file;
  57.                 else
  58.                     file = "http://" + u.getHost() + file;
  59.             }
  60.             
  61.             // end proxy support
  62.             
  63.             command = command + file + " HTTP/1.0";
  64.             SocketFactory.writeString(out,command);
  65.             Log.log(Log.LOG_LEVEL_NORMAL,"Request: " + command );
  66.             
  67.             // Process client headers
  68.             
  69.             if ( post!=null )
  70.                 clientHeaders.set("Content-Length","" + post.length());
  71.             
  72.             clientHeaders.set("Host", u.getHost() );
  73.             
  74.             i=0;
  75.             headers = new StringBuffer();
  76.             do {
  77.                 a = clientHeaders.get(i++);
  78.                 if ( a!=null ) {
  79.                     headers.append(a.getName());
  80.                     headers.append(": ");
  81.                     headers.append(a.getValue());
  82.                     headers.append("/r/n");
  83.                     Log.log(Log.LOG_LEVEL_TRACE,"Client Header:" + a.getName() + "=" + a.getValue() );
  84.                 }
  85.             } while ( a!=null );
  86.             
  87.             Log.log(Log.LOG_LEVEL_DUMP,"Writing client headers:" + headers.toString() );
  88.             if ( headers.length()>=0 )
  89.                 out.write(headers.toString().getBytes() );
  90.             
  91.             // Send a blank line to signal end of HTTP headers
  92.             SocketFactory.writeString(out,"");
  93.             if ( post!=null ) {
  94.                 Log.log(Log.LOG_LEVEL_TRACE,"Socket Post(" + post.length() + " bytes):" + new String(post) );
  95.                 out.write( post.getBytes()  );
  96.             }
  97.             
  98.             /* Read the result */
  99.             /* First read HTTP headers */
  100.             
  101.             header.setLength(0);
  102.             int chars = 0;
  103.             boolean done = false;
  104.             
  105.             while ( !done ) {
  106.                 int ch;
  107.                 
  108.                 ch = in.read();
  109.                 if ( ch==-1 )
  110.                     done=true;
  111.                 
  112.                 switch ( ch ) {
  113.                     case '/r':
  114.                         break;
  115.                     case '/n':
  116.                         if ( chars==0 )
  117.                             done =true;
  118.                         chars=0;
  119.                         break;
  120.                     default:
  121.                         chars++;
  122.                         break;
  123.                 }
  124.                 
  125.                 header.append((char)ch);
  126.             }
  127.             
  128.             // now parse the headers and get content length
  129.             parseHeaders();
  130.             Attribute acl = serverHeaders.get("Content-length");
  131.             int contentLength=0;
  132.             try {
  133.                 if ( acl!=null )
  134.                     contentLength = Integer.parseInt(acl.getValue());
  135.             } catch ( Exception e ) {
  136.                 Log.logException("Bad value for content-length:",e);
  137.             }
  138.             
  139.             int max;
  140.             if ( maxBodySize!=-1 )
  141.                 max = Math.min(maxBodySize,contentLength );
  142.             else
  143.                 max = contentLength;
  144.             
  145.             if ( max<1 )
  146.                 max=-1;
  147.             
  148.             ByteList byteList = new ByteList();
  149.             byteList.read(in,max);
  150.             body = byteList.detach();
  151.             Log.log(Log.LOG_LEVEL_DUMP,"Socket Page Back:" + new String(body) + "/r/n" );
  152.             
  153.             if ( (err>=400) && (err<=599) ) {
  154.                 Log.log(Log.LOG_LEVEL_ERROR,"HTTP Exception:" + response );
  155.                 throw new HTTPException(response);
  156.             }
  157.             
  158.         }
  159.         
  160.         // Cleanup
  161.         finally {
  162.             if ( out!=null ) {
  163.                 try {
  164.                     out.close();
  165.                 } catch ( Exception e ) {
  166.                 }
  167.             }
  168.             
  169.             if ( in!=null ) {
  170.                 try {
  171.                     in.close();
  172.                 } catch ( Exception e ) {
  173.                 }
  174.             }
  175.             
  176.             if ( socket!=null ) {
  177.                 try {
  178.                     socket.close();
  179.                 } catch ( Exception e ) {
  180.                 }
  181.             }
  182.         }
  183.     }
  184.     
  185.     HTTP copy() {
  186.         return new HTTPSocket();
  187.     }
  188.     
  189.     
  190.     /**
  191.      * This method is called to add the user authorization headers
  192.      * to the HTTP request.
  193.      */
  194.     protected void addProxyAuthHeader() {
  195.         if( (SocketFactory.getProxyUID()!=null) && (SocketFactory.getProxyUID().length()>0) ) {
  196.             String hdr = SocketFactory.getProxyUID() + ":" + SocketFactory.getProxyPWD()==null?"":SocketFactory.getProxyPWD();
  197.             String encode = URLUtility.base64Encode(hdr);
  198.             clientHeaders.set("Proxy-Authorization","Basic " + encode );
  199.         }
  200.     }       
  201. }

SocketFactory类的代码清单如下:

  1. /*
  2.  * SocketFactory.java
  3.  *
  4.  * Created on August 19, 2003, 7:15 PM
  5.  */
  6. package com.heaton.bot;
  7. import java.net.*;
  8. import java.io.*;
  9. /**
  10.  *
  11.  * @author  jheaton
  12.  */
  13. public class SocketFactory {
  14.     
  15.     private static String proxyHost;
  16.     private static int proxyPort;
  17.     private static String proxyUID;
  18.     private static String proxyPWD;
  19.     
  20.     public static void setProxyHost(String proxyHost) {
  21.         SocketFactory.proxyHost = proxyHost;
  22.     }
  23.     
  24.     public static void setProxyUID(String proxyUID) {
  25.         SocketFactory.proxyUID = proxyUID;
  26.     }
  27.     
  28.     public static void setProxyPWD(String proxyPWD) {
  29.         SocketFactory.proxyPWD = proxyPWD;
  30.     }
  31.     
  32.     public static void setProxyPort(int id) {
  33.         SocketFactory.proxyPort = id;
  34.     }
  35.     
  36.     public static String getProxyHost() {
  37.         return proxyHost;
  38.     }
  39.     
  40.     public static String getProxyUID() {
  41.         return proxyUID;
  42.     }
  43.     
  44.     public static String getProxyPWD() {
  45.         return proxyPWD;
  46.     }
  47.     
  48.     public static int getProxyPort() {
  49.         return proxyPort;
  50.     }
  51.     
  52.     public static boolean useProxy() {
  53.         return( (proxyHost!=null) && (proxyHost.length()>0) );
  54.     }
  55.     
  56.     public static void writeString(OutputStream out,String str)
  57.     throws java.io.IOException {
  58.         out.write( str.getBytes() );
  59.         out.write( "/r/n".getBytes() );
  60.         Log.log(Log.LOG_LEVEL_TRACE,"Socket Out:" + str );
  61.     }
  62.     
  63.     public static String receive(InputStream in)
  64.     throws IOException {
  65.         String result = "";
  66.         boolean done = false;
  67.         
  68.         while (!done) {
  69.             int ch = in.read();
  70.             switch (ch) {
  71.                 case 13:
  72.                     break;
  73.                 case 10:
  74.                     done=true;
  75.                     break;
  76.                 default:
  77.                     result+=(char)ch;
  78.             }
  79.             
  80.         }
  81.         //System.out.println("Recv:" + result );
  82.         return result;
  83.     }    
  84.     
  85.     
  86.     static public Socket getSocket(String host,int port,boolean https) 
  87.     throws IOException 
  88.     {
  89.         Socket socket;
  90.         
  91.         if( useProxy() ) {
  92.             Log.log(Log.LOG_LEVEL_NORMAL,"Connection to: " + proxyHost+"(" + proxyPort + ")" );
  93.             try {
  94.                 socket = new Socket(proxyHost,proxyPort);
  95.             }
  96.             catch(IOException e) {
  97.                 throw new IOException("Proxy connect failed: " + e.getMessage() );
  98.             }
  99.             
  100.             if( https ) {
  101.                 OutputStream out = socket.getOutputStream();
  102.                 InputStream in = socket.getInputStream();
  103.                 
  104.                 String str = "CONNECT " + host + ":" + port + " HTTP/1.0";
  105.                 Log.log(Log.LOG_LEVEL_NORMAL,"Tunnel: " + str );
  106.                 writeString(out,str);
  107.                 str = "User-Agent: Java Bot Package";
  108.                 writeString(out,str);
  109.                 writeString(out,"");
  110.                 out.flush();
  111.                 do {
  112.                     str = receive(in);
  113.                     Log.log(Log.LOG_LEVEL_TRACE,"Tunnel handshake: " + str );
  114.                 } while(str.length()>0);
  115.                 
  116.                 socket = SSL.getSSLSocket(host,port);
  117.             }
  118.         }
  119.         else {
  120.             if( https )
  121.                 socket = SSL.getSSLSocket(host,port);
  122.             else
  123.                 socket = new Socket(host,port);
  124.         }
  125.         return socket;
  126.         
  127.     }
  128.     
  129. }

感觉在装门面啊……

阅读全文
0 0

相关文章推荐

img
取 消
img