Not written by me, I found it : HerePHP Code:
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Connection implements Serializable {
static final long serialVersionUID = 1L;
String domain, referer;
Map<String,String> cookies;
static String rpUseragent = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.14) Gecko/20080509 Firefox/2.0.0.14";
static String rpAcceptText = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
static String rpAcceptPng = "image/png,image/*;q=0.8,*/*;q=0.5";
static String rpAcceptLanguage = "en-us,en;q=0.5";
static String rpAcceptEncoding = "gzip, deflate";
static String rpAcceptCharset = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
static String rpKeepAlive = "300";
static String rpConnection = "keep-alive";
static String rpContentType = "application/x-www-form-urlencoded";
public Connection( String domain, Map<String,String> cookies, String referer ) {
this.domain = domain;
this.cookies = cookies;
this.referer = referer;
}
public Connection( String domain, Map<String,String> cookies ) {
this( domain, cookies, null );
}
public Connection( String domain, String referer ) {
this( domain, new HashMap<String,String>(), referer );
}
public Connection( String domain ) {
this( domain, new HashMap<String,String>(), null );
}
public String get( String url ) {
if( url.charAt( 0 ) == '/' )
url = domain + url;
try {
HttpURLConnection conn = (HttpURLConnection)( new URL( url.replaceAll( " ", "%20" ) ).openConnection() );
setRequestProperties( conn );
conn.setRequestMethod( "GET" );
referer = url;
return read( conn );
} catch( IOException e1 ) {
e1.printStackTrace();
return null;
}
}
public String post( String url, String[][] data ) {
if( url.charAt( 0 ) == '/' )
url = domain + url;
try {
HttpURLConnection conn = (HttpURLConnection)( new URL( url.replaceAll( " ", "%20" ) ).openConnection() );
setRequestProperties( conn );
conn.setRequestMethod( "POST" );
conn.setDoOutput( true );
StringBuilder sb = new StringBuilder();
for( int i = 0; i < data[0].length; i++ )
sb.append( URLEncoder.encode( data[0][i], "UTF-8" ) ).append( '=' ).append( URLEncoder.encode( data[1][i], "UTF-8" ) ).append( '&' );
conn.setRequestProperty( "Content-Type", rpContentType );
conn.setRequestProperty( "Content-Length", Integer.toString( sb.length()-1 ) );
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) ) );
out.write( sb.substring( 0, sb.length()-1 ) );
out.close();
referer = url;
return read( conn );
} catch( IOException e1 ) {
e1.printStackTrace();
return null;
}
}
public BufferedImage getImage( String url ) {
try {
HttpURLConnection conn = (HttpURLConnection)( new URL( ( url.charAt( 0 ) == '/' ? domain+url : url ).replaceAll( " ", "%20" ) ).openConnection() );
setRequestProperties( conn );
conn.setRequestMethod( "GET" );
conn.setRequestProperty( "Accept", rpAcceptPng );
return ImageIO.read( conn.getInputStream() );
} catch( IOException e1 ) {
e1.printStackTrace();
return null;
}
}
public boolean hasCookie( String key ) {
return cookies.containsKey( key );
}
public String getCookieString() {
StringBuilder sb = new StringBuilder();
for( String s : cookies.keySet() )
sb.append( s ).append( '=' ).append( cookies.get( s ) ).append( ';' );
return sb.toString();
}
private void setRequestProperties( HttpURLConnection conn ) {
conn.setInstanceFollowRedirects( false );
conn.setRequestProperty( "User-Agent", rpUseragent );
conn.setRequestProperty( "Accept", rpAcceptText );
conn.setRequestProperty( "Accept-Language", rpAcceptLanguage );
conn.setRequestProperty( "Accept-Encoding", rpAcceptEncoding );
conn.setRequestProperty( "Accept-Charset", rpAcceptCharset );
conn.setRequestProperty( "Keep-Alive", rpKeepAlive );
conn.setRequestProperty( "Connection", rpConnection );
if( referer != null && referer.length() != 0 )
conn.setRequestProperty( "Referer", referer );
if( cookies != null && cookies.size() != 0 )
conn.setRequestProperty( "Cookie", getCookieString() );
}
private String read( HttpURLConnection conn ) throws IOException {
BufferedReader in = null;
if( conn.getContentEncoding() == null )
in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
else
if( conn.getContentEncoding().equalsIgnoreCase( "gzip" ) )
in = new BufferedReader( new InputStreamReader( new GZIPInputStream( conn.getInputStream() ) ) );
else if( conn.getContentEncoding().equalsIgnoreCase( "deflate" ) )
in = new BufferedReader( new InputStreamReader( new InflaterInputStream( conn.getInputStream(), new Inflater( true ) ) ) );
StringBuilder sb = new StringBuilder();
String s;
while( ( s = in.readLine() ) != null )
sb.append( s ).append( '\n' );
putCookies( conn.getHeaderFields().get( "Set-Cookie" ) );
return sb.toString();
}
private void putCookies( List<String> cookieList ) {
if( cookieList == null )
return;
int index;
for( String cookie : cookieList )
cookies.put( cookie.substring( 0, index = cookie.indexOf( '=' ) ), cookie.substring( index+1, cookie.indexOf( ';', index ) ) );
}
}
Results 1 to 5 of 5
Thread: [Java] Connection (Http) Class
- 28 Jan. 2011 01:05am #1
[Java] Connection (Http) Class
Shh, I'm watching My little pony.
- 28 Jan. 2011 01:15am #2
- 28 Jan. 2011 01:19am #3
Sounds like it'd be a good thing. We can then make multi platform bots
Shh, I'm watching My little pony.
- 28 Jan. 2011 01:26am #4
- 28 Jan. 2011 01:28am #5
Use eclipse
Shh, I'm watching My little pony.