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 Best Breakfast Food?
10-23-2008 12:00 PM
Last post by candical
Today 10:44 PM
260 Replies, 2,268 Views
Go to first new post Halo 3 sig
Today 08:47 PM
by N1nja
Last post by Tsubi
Today 10:43 PM
12 Replies, 44 Views
Go to first new post Do you have the Adam's...
Today 05:50 PM
by oneone
Last post by jsndin
Today 10:43 PM
13 Replies, 61 Views
Go to first new post Cake is delicious!
10-20-2008 01:03 PM
Last post by candical
Today 10:42 PM
137 Replies, 1,140 Views
Go to first new post Neopets.com disallows...
Today 09:16 PM
by Bammeh
Last post by %Access Denied%
Today 10:39 PM
1 Replies, 11 Views
View Single Post

 Java socket class w/ proxy & cookie support
Old 04-08-2007, 09:17 PM   #1 (permalink)
sockopen
Guest

 
Posts: n/a
GPoints: 0 [Check]
iTrader: / %
Java socket class w/ proxy & cookie support

Code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;

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

public class HTTPWrapper {
    
    private static String strCookies;
    private static String strHead;
    private static String strHTML;
    public static String strProxy;
    
    public static void initHTTPWrapper() {
        strProxy = "";
        strCookies = "";
    }
    
    public static String Request(String URL, String PostData, String Referer) {
        StringBuffer strBuffer = new StringBuffer(); int Port; Socket sock;
        String Method; if (PostData != "") Method = "POST"; else Method = "GET";
        try{
            URL = URL.toLowerCase();
            String tHost = "", File = ""; 
            if (URL.startsWith("http://")) URL = URL.replaceFirst("http://", "");
            if (strProxy != "") {
                Port = 80;
                if (URL.endsWith("/")) {
                    URL = URL.substring(0, URL.length() - 1);
                    tHost = URL; File = "/";
                } else if (URL.indexOf("/") == -1) { 
                    tHost = URL; File = "/"; 
                } else {
                    String[] splitURL = URL.split("[/]", 2);
                    tHost = splitURL[0]; File = "/" + splitURL[1];
                }
                sock = new Socket(tHost, Port);
            }
            else {
                String[] splitProxy = strProxy.split("[:]");
                Port = Integer.parseInt(splitProxy[1]);
                File = URL; tHost = splitProxy[0];
                sock = new Socket(tHost, Port);
            }
            
            BufferedReader bufferIn = new BufferedReader(new InputStreamReader(sock.getInputStream(), "UTF-8"));
            BufferedWriter bufferOut = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
            
            bufferOut.write(Method + " " + File + " HTTP/1.1"
                            + "\r\nHost: " + tHost
                            + "\r\nUser-Agent: Mozilla/5.0 (Windows; you; Windows NT 5.1; en-US; rv:7.7.7) Gecko/20050207 Firefox/1.0"
                            + "\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
                            + "\r\nAccept-Language: en-us,en;q=0.5"
                            //+ "\r\nAccept-Encoding: gzip,deflate"
                            + "\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            if (Referer != "0")
                bufferOut.write("\r\nReferer: " + Referer);
            if (strCookies != "")
                bufferOut.write("\r\nCookie: " + strCookies);
            if (Method == "POST") {
                bufferOut.write("\r\nContent-Type: application/x-www-form-urlencoded"
                                + "\r\nContent-Length: " + PostData.length());
            }
            bufferOut.write("\r\nConnection: close"
                            + "\r\n\r\n");
            if (Method == "POST") bufferOut.write(PostData + "\r\n");
            bufferOut.flush();
            
            int intReceived;
            while((intReceived = bufferIn.read()) != -1) {
                strBuffer.append((char) intReceived);
            }
            String[] splitResponse = strBuffer.toString().split("\n\r", 2);
            strHead = splitResponse[0]; strHTML = splitResponse[1];
            bufferIn.close();
            bufferOut.close();

            if (strHead.indexOf("Set-Cookie: ") != -1) {
                StringBuffer strTempCookies = new StringBuffer();
                strTempCookies.append(strCookies);
                if (strTempCookies.length() > 0) strTempCookies.append("; ");
                String arrCookies[] = strHead.split("Set-Cookie: ");
                for (int I = 1; I < arrCookies.length - 1; I++) { 
                    String arrSplitCookies[] = arrCookies[i].split("[=;]");
                    if (strCookies.indexOf(arrSplitCookies[0]) != -1) {
                        if (strCookies.indexOf(arrSplitCookies[0] + "=" + arrSplitCookies[1]) == -1)
                            strTempCookies.replace(strCookies.indexOf(arrSplitCookies[0]) + arrSplitCookies[0].length() + 1, strCookies.indexOf(";", strCookies.indexOf(arrSplitCookies[0])), arrSplitCookies[1]);
                    }
                    else
                        strTempCookies.append(arrSplitCookies[0] + "=" + arrSplitCookies[1] + "; "); 
                }
                if (strTempCookies.indexOf("; ", strTempCookies.length() - 3) != -1)
                    strTempCookies.delete(strTempCookies.length() - 2, strTempCookies.length());
                strCookies = strTempCookies.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;

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

Usage (POST):
HTTPWrapper.Request("www.neopets.com/login.phtml", "Username=Password=", "www.Referer.com");

Set a Proxy:
HTTPWrapper.strProxy = "127.0.0.1:80";
  Reply With Quote
 
Powered by vBadvanced CMPS v3.1.0

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