Go Back   Gaming Gutter


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

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post GG NIGHT CREW, CHECK IN!
12-15-2008 04:59 AM
by Bunneh
Last post by DarkenedSky
Today 11:04 PM
3,889 Replies, 16,440 Views
Go to first new post What is WRONG with you!
01-04-2009 08:58 AM
by Zombies
Last post by DarkenedSky
Today 11:03 PM
44 Replies, 489 Views
Go to first new post Halo 3 sig
Today 08:47 PM
by N1nja
Last post by Tsubi
Today 10:59 PM
14 Replies, 44 Views
Go to first new post Let's say I have script,...
Today 10:54 PM
by Bammeh
Last post by Bammeh
Today 10:54 PM
0 Replies, 1 Views
Go to first new post Favorite Junkfood?
10-26-2008 01:36 PM
Last post by candical
Today 10:53 PM
211 Replies, 1,181 Views
View Single Post

 Java URL class w/ GZip, Deflate & cookie support
Old 04-08-2007, 09:18 PM   #1 (permalink)
sockopen
Guest

 
Posts: n/a
GPoints: 0 [Check]
iTrader: / %
Java URL class w/ GZip, Deflate & cookie support

Code:
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

import java.io.IOException;
import java.net.UnknownHostException;

public class HTTPWrapper {
	
	public static String strCookies;
	private static String strHTML;
	
	public static void initHTTPWrapper() {
		strCookies = "";
	}
	
	public static String Request(String URL, String PostData, String Referer) {
		String method; if (PostData != "") method = "POST"; else method = "GET";
		try{
			URL url = new URL("http://" + URL);
			HttpURLConnection http = (HttpURLConnection) url.openConnection();
			http.setFollowRedirects(false);
			http.setRequestMethod(method);
			http.setRequestProperty("Host", url.getHost());
			http.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3");
			http.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
			http.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			http.setRequestProperty("Accept-Encoding", "gzip,deflate");
			http.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			http.setRequestProperty("Keep-Alive", "300");
			http.setRequestProperty("Connection", "keep-alive");
			if (Referer != "0")
				http.setRequestProperty("Referer", Referer);
			if (strCookies != "")
				http.setRequestProperty("Cookie", strCookies);
			if (method == "POST") {
				http.setDoOutput(true);
				http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
				http.setRequestProperty("Content-Length", "" + PostData.length());
				OutputStreamWriter writePost = new OutputStreamWriter(http.getOutputStream());
				writePost.write(PostData);
				writePost.flush();
			}
			http.connect();
			String encoding = http.getContentEncoding();
			InputStream receiving = null;
			if (encoding.equalsIgnoreCase("gzip"))
				receiving = new GZIPInputStream(http.getInputStream());
			else if (encoding.equalsIgnoreCase("deflate"))
				receiving = new InflaterInputStream(http.getInputStream(), new Inflater(true));
			else
				receiving = http.getInputStream();
			int intReceived; StringBuffer strBuffer = new StringBuffer();
			while((intReceived = receiving.read()) != -1) {
				strBuffer.append((char) intReceived);
			}
			receiving.close();
			strHTML = strBuffer.toString();
			strBuffer.delete(0, strBuffer.length()); strBuffer.append(strCookies);
			if (strCookies.length() > 3) strBuffer.append("; ");
			for (int i=0;; i++) {
				if (http.getHeaderFieldKey(i) == null && http.getHeaderField(i) == null) {
					break;
				} else if ("Set-Cookie".equalsIgnoreCase(http.getHeaderFieldKey(i))) {
					String[] fields = http.getHeaderField(i).split(";\\s*");
					String[] keyvalue = fields[0].split("=");
					if (strBuffer.indexOf(keyvalue[0] + "=") == -1)
						strBuffer.append(keyvalue[0] + "=" + keyvalue[1] + "; "); 
					else
						strBuffer.replace(strBuffer.indexOf(keyvalue[0]), strBuffer.indexOf(";", strBuffer.indexOf(keyvalue[0])), keyvalue[0] + "=" + keyvalue[1]);
				}
			}
			if (strBuffer.indexOf("; ", strBuffer.length() - 3) != -1)
				strBuffer.delete(strBuffer.length() - 2, strBuffer.length());
			strCookies = strBuffer.toString();
		} catch (Exception e) {}
		return strHTML;
	}
}
You should initiate the HTTPWrapper before Request'ing, which initiates the Cookies and Proxy variables and will prevent our class from throwing an exception. HTTPWrapper.initHTTPWrapper;

Proxy support and everything is all easily done with the HttpURLConnection ('http' in this code snippet I wrote).

Usage (GET):
HTTPWrapper.Request("www.neopets.com", "", "www.Referer.com");

Usage (POST):
HTTPWrapper.Request("www.neopets.com/login.phtml", "Username=Password=", "www.Referer.com");
  Reply With Quote
 
Powered by vBadvanced CMPS v3.1.0

All times are GMT -7. The time now is 11:06 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.16074610 seconds (100.00% PHP - 0% MySQL) with 15 queries