001 package com.croftsoft.core.net.http; 002 003 import java.io.*; 004 import java.net.*; 005 006 import com.croftsoft.core.io.Parser; 007 import com.croftsoft.core.io.StringCoder; 008 009 /********************************************************************* 010 * HTTP operations. 011 * 012 * @version 013 * 2003-06-05 014 * @since 015 * 1999-12-19 016 * @author 017 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 018 *********************************************************************/ 019 020 public final class HttpLib 021 ////////////////////////////////////////////////////////////////////// 022 ////////////////////////////////////////////////////////////////////// 023 { 024 025 public static final String APPLICATION_OCTET_STREAM 026 = "application/octet-stream"; 027 028 ////////////////////////////////////////////////////////////////////// 029 ////////////////////////////////////////////////////////////////////// 030 031 public static void main ( String [ ] args ) 032 throws Exception 033 ////////////////////////////////////////////////////////////////////// 034 { 035 System.out.println ( 036 post ( 037 new URL ( args [ 0 ] ), 038 "get".getBytes ( StringCoder.US_ASCII ), 039 "HttpLib/1.0", 040 "text/plain", 041 new StringCoder ( StringCoder.US_ASCII ) ) ); 042 } 043 044 ////////////////////////////////////////////////////////////////////// 045 ////////////////////////////////////////////////////////////////////// 046 047 public static Object post ( 048 URL url, 049 byte [ ] bytes, 050 String userAgent, 051 String contentType, 052 Parser parser ) 053 throws IOException 054 ////////////////////////////////////////////////////////////////////// 055 { 056 HttpURLConnection httpURLConnection 057 = ( HttpURLConnection ) url.openConnection ( ); 058 059 httpURLConnection.setRequestMethod ( "POST" ); 060 061 if ( userAgent != null ) 062 { 063 httpURLConnection.setRequestProperty ( "User-Agent", userAgent ); 064 } 065 066 if ( contentType != null ) 067 { 068 httpURLConnection.setRequestProperty ( 069 "Content-Type", contentType ); 070 } 071 072 httpURLConnection.setRequestProperty ( 073 "Content-Length", Integer.toString ( bytes.length ) ); 074 075 httpURLConnection.setDoOutput ( true ); 076 077 OutputStream outputStream = httpURLConnection.getOutputStream ( ); 078 079 BufferedOutputStream bufferedOutputStream 080 = new BufferedOutputStream ( outputStream ); 081 082 bufferedOutputStream.write ( bytes ); 083 084 bufferedOutputStream.close ( ); 085 086 if ( parser != null ) 087 { 088 if ( httpURLConnection.getResponseCode ( ) 089 == HttpURLConnection.HTTP_OK ) 090 { 091 int contentLength = httpURLConnection.getContentLength ( ); 092 093 InputStream inputStream = httpURLConnection.getInputStream ( ); 094 095 BufferedInputStream bufferedInputStream 096 = new BufferedInputStream ( inputStream ); 097 098 Object object 099 = parser.parse ( bufferedInputStream, contentLength ); 100 101 bufferedInputStream.close ( ); 102 103 return object; 104 } 105 } 106 107 return null; 108 } 109 110 public static int post ( 111 String host, 112 String path, 113 byte [ ] bytes ) 114 throws IOException 115 ////////////////////////////////////////////////////////////////////// 116 { 117 return post ( host, 80, path, "HttpLib/1.00", 118 "application/x-www-form-urlencoded", bytes ); 119 } 120 121 public static int post ( 122 String host, 123 int port, 124 String path, 125 String userAgent, 126 byte [ ] bytes ) 127 throws IOException 128 ////////////////////////////////////////////////////////////////////// 129 { 130 return post ( host, port, path, userAgent, 131 "application/x-www-form-urlencoded", bytes ); 132 } 133 134 public static int post ( 135 String host, 136 int port, 137 String path, 138 String userAgent, 139 String contentType, 140 byte [ ] bytes ) 141 throws IOException 142 ////////////////////////////////////////////////////////////////////// 143 { 144 HttpConnection httpConnection = new HttpConnection ( 145 host, port, path,userAgent, contentType, bytes, null ); 146 147 httpConnection.post ( ); 148 149 return httpConnection.getResponseCode ( ); 150 } 151 152 ////////////////////////////////////////////////////////////////////// 153 ////////////////////////////////////////////////////////////////////// 154 155 private HttpLib ( ) { } 156 157 ////////////////////////////////////////////////////////////////////// 158 ////////////////////////////////////////////////////////////////////// 159 }