Go Back   Gaming Gutter > Non-Gaming > Programming > Source Code


Source Code - Have a source code/project files you want to post? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post What Sport Do You Prefer?
08-05-2008 08:33 PM
Last post by maximous
Today 11:17 PM
127 Replies, 1,072 Views
Go to first new post Whats your favorite...
08-06-2008 08:59 PM
Last post by maximous
Today 11:16 PM
105 Replies, 786 Views
Go to first new post Soo.... Im gone for like...
Today 10:44 PM
Last post by Bex
Today 11:14 PM
2 Replies, 3 Views
Go to first new post What do you do when your...
10-25-2008 12:23 AM
Last post by Dday
Today 11:03 PM
117 Replies, 751 Views
Go to first new post Why Make us post here?
11-19-2008 03:54 PM
by shep
Last post by Dday
Today 11:02 PM
12 Replies, 81 Views
Reply
 
LinkBack Thread Tools Display Modes

 [Java] HttpRequestWrapper and Example
Old 02-23-2008, 03:27 PM   #1 (permalink)
Full Member

unlimitedorb is offline
 
Join Date: Feb 2008
Posts: 25
GPoints: 29
iTrader: 0 / 0%
unlimitedorb Is gaining popularity
Rep Power: 3
[Java] HttpRequestWrapper and Example

Cookie support has been stripped out to remove the need for the jCookie library. Support can be built back in rather easily, or you can manually set them with the setCookie() method (not recommended.)

Save the below as HttpRequestWrapper.java and compile.
Code:
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
//import com.sonalb.net.http.cookie.*;

import java.io.IOException;


/*
 * An HTTP wrapper class implementing the Singleton Design pattern.
 * Gzip and Deflate compression algorithms are supported.
 * Proxy and Cookie support is included.
 *
 * @author unlimitedorb
 * @date 02/15/08
 */
public class HttpRequestWrapper {
	private URL url;
	private String source;
	private URL proxy;
	private HttpURLConnection connection;
	private String cookie = "";
	
	private HttpRequestWrapper() {
	}
	
	private static class HttpRequestWrapperContainer {
		private static final HttpRequestWrapper INSTANCE = new HttpRequestWrapper();
	}
	
	public static HttpRequestWrapper getInstance() {
		return HttpRequestWrapperContainer.INSTANCE;
	}
	
	public String readWebPage(String address, String referer, String postData) {
		try {
			this.url = new URL(address);
			if(proxy != null) {
				SocketAddress addr = new InetSocketAddress(proxy.getHost(), proxy.getPort());
				Proxy proxyObject = new Proxy(Proxy.Type.HTTP, addr);
				connection = (HttpURLConnection) url.openConnection(proxyObject);
			} else {
				connection = (HttpURLConnection) url.openConnection();
                           }
			connection.setFollowRedirects(false);
			connection.setRequestProperty("Host", url.getHost());
			connection.setRequestProperty("Accept", "*/*");
			connection.setRequestProperty("Accept-Language", "en-us");
			connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
			connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
			if(!postData.isEmpty()) {
				connection.setRequestMethod("POST");
				connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
				connection.setRequestProperty("Content-Length", "" + postData.length());
			} else {
				connection.setRequestMethod("GET");
			}
			if(!referer.isEmpty()) {
				connection.setRequestProperty("Referer", referer);
			}
			if(!cookie.isEmpty()) {
				connection.setRequestProperty("Cookie", cookie);
			}
			if(!postData.isEmpty()) {
				connection.setDoOutput(true);
				OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
				out.write(postData);
				out.flush();
				out.close();
			}
			
			String encoding = connection.getContentEncoding();
			InputStream stream;
			if(encoding.equalsIgnoreCase("gzip"))
				stream = new GZIPInputStream(connection.getInputStream());
			else if(encoding.equalsIgnoreCase("deflate"))
				stream = new InflaterInputStream(connection.getInputStream());
			else
				stream = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
			String line;
			while((line = reader.readLine()) != null) {
				source += line;
			}
			reader.close();
		} catch(IOException e) {
			e.printStackTrace();
		}
		return source;
	}
	
	public void setProxy(URL proxy) {
		this.proxy = proxy;
	}
	
	public String getSource() {
	    return this.source;
	}
	
	public void setCookies(String cookie) {
	    this.cookie = cookie;
	}
}
To set cookies call HttpWrapper.getInstance().setCookie("Cookie data");
To set the proxy call: HttpWrapper.getInstance.setProxy(new URL("127.0.0.1:8080"));

Hope it's helpful to someone.

Last edited by unlimitedorb; 03-02-2008 at 09:14 PM.
  Reply With Quote

 
Old 02-23-2008, 03:30 PM   #2 (permalink)
Banned

Undisclosed FireWrath is offline
 
Join Date: Oct 2007
Posts: 3,209
GPoints: 56
iTrader: 1 / 100%
FireWrath Total CelebrityFireWrath Total CelebrityFireWrath Total CelebrityFireWrath Total CelebrityFireWrath Total CelebrityFireWrath Total Celebrity
Rep Power: 0
Oh, I rather like this one, not because I need a wrapper, but because I includes what I've been meaning to learn in Java. Good work.
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 11:17 PM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.09202600 seconds (100.00% PHP - 0% MySQL) with 20 queries