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 Lil Wayne
10-21-2008 09:33 PM
Last post by April<33
Today 01:34 AM
44 Replies, 365 Views
Go to first new post mOBSCENE's score sender...
Today 01:11 AM
Last post by April<33
Today 01:30 AM
3 Replies, 12 Views
Go to first new post Which Neopet hack makes...
11-09-2008 02:14 PM
by weezer
Last post by April<33
Today 01:26 AM
27 Replies, 835 Views
Go to first new post neopets account info...
Yesterday 07:26 AM
by lyro34
Last post by April<33
Today 01:25 AM
4 Replies, 65 Views
Go to first new post Free SS list
11-16-2008 09:37 AM
Last post by April<33
Today 01:24 AM
8 Replies, 254 Views
Reply
 
LinkBack Thread Tools Display Modes

 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

 
Old 03-02-2008, 09:54 PM   #2 (permalink)
Full Member

unlimitedorb is offline
 
Join Date: Feb 2008
Posts: 25
GPoints: 29
iTrader: 0 / 0%
unlimitedorb Is gaining popularity
Rep Power: 3
Damn! This is quite literally some of the ugliest Java code I've ever encountered...
Everything from naming conventions to methods being used resemble the work of a grade A scumbag.

Judging from your posts, you're a Perl fan. My advice would be for you to not write Perl in Java. Perl was meant to be a "get the job done" type of language. In Java, you want code that will stand the test of time. Only reason why I'm picking on you is because you're one of the few that doesn't solely use VB for projects.
  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 01:34 AM.


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.09299207 seconds (100.00% PHP - 0% MySQL) with 17 queries