I fixed some major bugs, for example, the cookie handling system was all wrong with the other one, and added a few more features - a more complete and realistic header with referrers, so here is the finalized first version of my HTTP wrapper in Java.
I didn't want to add this as a reply along with the other thread because this also has documentation and stuff.
Here it is!
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class Connection {
Socket MyClient = null;
Scanner input = null;
PrintStream output = null;
String host = "";
static String mainCookies = "";
String referrer = "http://www.google.com";
public Connection (String host)
{
this. host = host;
try {
MyClient = new Socket(host, 80);
input = new Scanner (MyClient.getInputStream());
output = new PrintStream (MyClient.getOutputStream ());
}
catch (IOException e)
{ System.out.println (e);}
}
public String request(String param)
{
try {
MyClient = new Socket(host, 80);
input = new Scanner (MyClient.getInputStream());
output = new PrintStream (MyClient.getOutputStream ());
}
catch (IOException e)
{ System.out.println (e);}
String [] items = param.split("\\|\\|");
String cookies = "";
String data = "";
String method = "";
String file = "";
String request = "";
String response = "";
String responseHeader = "";
String responseData = "";
//Parses paramter for information.
for (int i = 0; i < items.length; i++)
{
if (items[i].indexOf("m::") != -1)
method = items[i].split("m::")[1];
if (items[i].indexOf("f::") != -1)
file = items[i].split("f::")[1];
if (items[i].indexOf("c::") != -1)
cookies = items[i].split("c::")[1];
if (items[i].indexOf("d::") != -1)
data = items[i].split("d::")[1];
}
//For manual cookie editing....weird.
//updateCookies(cookies) ;
//*******************************************************/
request += (method+" /"+file+" HTTP/1.1\r\n");
request += ("Host: "+host+"\r\n");
request += ("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3\r\n");
request += ("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/ *;q=0.5\r\n");
request += ("Accept-Language: en-us,en;q=0.5\r\n");
request += ("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
request += ("Keep-Alive: 300\r\n");
request += ("Connection: keep-alive\r\n");
request += ("Referer: "+referrer+"\r\n");
if (!mainCookies.equals(""))
request += ("Cookie: "+ mainCookies+"\r\n");
if (!data.equals(""))
{
request += ("Content-Length: "+data.length()+"\r\n");
request += ("Content-Type: application/x-www-form-urlencoded\r\n");
request += ("\r\n");
request += (data+"\r\n");
}
request += ("\r\n");
//updating previously accessed website, i.e. referrer:
referrer = "http://"+host+"/"+file;
try
{
output. print(request);
while (input.hasNext())
response += input.nextLine() + "\n";
}
catch (Exception e)
{
System.out.println (e);
}
output.close( );
input.close();
responseHeader = response.split("Content-Type")[0];
responseData = response.split("Content-Type")[1];
//Parsing out the cookies
cookies = "";
while (responseHeader.indexOf("Set-Cookie: ", 10) != -1)
{
responseHeader = responseHeader.substring(responseHeader.indexOf("Set-Cookie: ", 10) - 1);
cookies += responseHeader.substring(responseHeader.indexOf(" "), responseHeader.indexOf(" ", 15));
}
if (!cookies.equals(""))
{
cookies = cookies.substring(1);
cookies = cookies.substring(0, cookies.length()-1);
}
updateCookies(cookies);
return request + "\n\n" + response;
}
public void updateCookies (String cookies)
{
int in;
String temp;
while (!cookies.equals(""))
{
//System.out. println("Cookies:"+cookies);
//System.out.println(mainCookies);
in = cookies.indexOf(";");
if (in == -1) //Last local cookie
{
temp = cookies.substring(0, cookies.indexOf("="));
if (mainCookies.indexOf(temp) != -1 && mainCookies.indexOf(temp) != 0) //needs updating
{
//System.out.println("\t\tCase 1");
if (mainCookies.split(temp)[1].indexOf(";") != -1)
mainCookies = mainCookies.split(temp)[0] + cookies + mainCookies.split(temp)[1].substring(mainCookies.split(temp)[ 1].indexOf(";"));
else
mainCookies += mainCookies.split(temp)[0] + cookies;
}
else if (mainCookies.indexOf(temp) == 0) // The special scenario when temp is first.
{
//System.out.println("\t\tCase 2");
if (mainCookies.indexOf(";") != -1)
mainCookies = cookies + mainCookies.substring(mainCookies.indexOf(";"));
else //Oooh, first and last!
mainCookies = cookies;
}
else //needs appending
{
//System.out.println("\t\tCase 3");
if (!mainCookies.equals(""))
mainCookies += ";"+cookies;
else
mainCookies = cookies;
}
cookies = "";
}
else //In case there are several cookies in the local string.
{
temp = cookies.substring(0, cookies.indexOf("="));
String oneCookie = cookies.split(";")[0]; //this is the individual cookie
if (mainCookies.indexOf(temp) != -1 && mainCookies.indexOf(temp) != 0) //Case: updating
{
//System.out.println("\t\tCase 1");
if (mainCookies.split(temp)[1].indexOf(";") != -1)
mainCookies = mainCookies.split(temp)[0] + oneCookie + mainCookies.split(temp)[1].substring(mainCookies.split(temp)[ 1].indexOf(";"));
else
mainCookies = mainCookies.split(temp)[0] + oneCookie;
}
else if (mainCookies.indexOf(temp) == 0) // The special scenario when temp is first.
{
//System.out.println("\t\tCase 2");
if (mainCookies.indexOf(";") != -1)
{
// System.out.println("\t\t2.1");
mainCookies = oneCookie + mainCookies.substring(mainCookies.indexOf(";"));
}
else //Oooh, first and last!
{
mainCookies = oneCookie;
//System.out.println(" \t\t2.2");
}
}
else
{
//System.out.println("\t\tCase 3");
if (!mainCookies.equals(""))
mainCookies += ";"+oneCookie;
else
mainCookies = oneCookie;
}
cookies = cookies.split(oneCookie+";")[1];
}
//System.out.println("\t\tCookies: "+cookies);
//System.out.println("\t\tmainCookies: "+mainCookies);
}
}
}
If you feel motivated, or intrigued to, use this wrapper class, then (I would be overjoyed!) here is documentation on how to use the class.
First, you need to instantiate an object/instance of the class:
Code:
Connection <variable name> = new Connection ("<domain>");
It is crucial to remember that the "<domain>" starts with "www." and ends with ".de/com/w/e else". NO "HTTP://" and NO ending it with "/"
Example:
(you need an account to see links)
(you need an account to see links)
After instantiating/declaring an object, there is only one major method to concern yourself with:
Code:
<your variable name>.request("<parameter string">);
This method only has 1 parameter. The argument is passed in a special way. I briefly explained this method in this thread.
Basically, this parameter is a special kind of String. All the data for the parameter is stored in one string. The different details are part of the string, separated by the sequence "||".
The different types of things that can be passed in as of now are: cookies, data (for POST variables), file/page, and method. I think it is easiest to understand this with examples:
CODE
.request("m:
OST||f::index.phtml" );
The above line will use the method POST and look for the file "index.phtml". Please note that it is NOT required to have a slash in front of the file section of the parameter. This is automatically taken care of inside the class..
CODE
.request("f::desert/shrine.phtml||m:
OST||d: :type=approach");
The above example would be used for going to Coltzan's shrine. The file it is trying to access is "desert/shrine.phtml". Again, notice that there is no "/" before "desert..". The method specified is "POST". The POST data that we are passing in is: "type=approach". Another important note: The order of the different data does NOT matter.
The official function header:
Code:
public String request(String param)
The returning string is the request along with the response.
PS. Please comment on my cookie updating method...I think, even though it looks big and bulky, it is efficient and has O(n) runtime..I think. Please critique it.
PPS. I will include GZip in the next version...or an updated version of this. I will also include ... fancy things...ideas are welcome.