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";