PDA

View Full Version : Good Java Wrapper?



Ryan~
03-18-2012, 09:52 PM
I need a good Java wrapper :)


Post/get, etc. You know :P Just the most recommended one!

Reemer
03-18-2012, 10:01 PM
I sent you that tutorial br0, it had thr wrapper in it. Additionally I posted it in the wrapper thread on here.

Edit: its in the source code sectionin the "various HTTP wrappers" thread

Ryan~
03-18-2012, 10:17 PM
I use that one, just making sure :)

I'm having a problem posting topics/replies on Neoboard with Java...



I'm getting the whole:


<div class="errormess" style="padding:10px;background-color:white;">
<b>Error: </b>You have been directed to this page from the wrong place! If you <b>KEEP</b> getting this error, chances are you have some security settings enabled that are not letting you play Neopets correctly.<br><br>Click <a href="/security_settings.phtml"><b>here</b></a> to see some tips that might help you fix this problem.</div><div style="padding:10px;background-color:black;"><a href="javascript:history.back();" style="color:white" class="errormess"><b><span class="errorpointer">&laquo;</span> Back</b></a></div></div>
</div>

I know if I resend the headers with Live [Only registered and activated users can see links] it works. but if I visit the link directly on Neo, I get that....how do I get around it like everyone else does? :(

Reemer
03-18-2012, 10:21 PM
That's usually a referral error, or the POST data isn't being sent correctly. I haven't used the Java wrapper very much though.

Ryan~
03-18-2012, 10:34 PM
It doesn't have an option for a referrer in the post data. There is a nother wrapper i've seen in some source just simply named [Only registered and activated users can see links] and the post looks like:


strHTML = wrap.post("[Only registered and activated users can see links]" , "boardType=reply&topic_id="+ boardID + "&board_id="+ boardNumber + "&next=1&message="+ bumpMessage + "&remLen=" + ((Integer)(400 - charCountBM)).toString(), "[Only registered and activated users can see links]" + boardID);


It's got the referrer right there at the end!

---------- Post added at 09:34 PM ---------- Previous post was at 09:23 PM ----------

If anyone can help me, PM me :)

Zachafer
03-18-2012, 11:20 PM
Sockopen's:


import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.[Only registered and activated users can see links]
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 [Only registered and activated users can see links] {

public static String strCookies;
private static String strHTML;

public static void init[Only registered and activated users can see links]() {
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("[Only registered and activated users can see links]" + URL);
[Only registered and activated users can see links] http = ([Only registered and activated users can see links]) url.openConnection();
[Only registered and activated users can see links](false);
[Only registered and activated users can see links](method);
[Only registered and activated users can see links]("Host", url.getHost());
[Only registered and activated users can see links]("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");
[Only registered and activated users can see links]("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/
png,*/*;q=0.5");
[Only registered and activated users can see links]("Accept-Language", "en-us,en;q=0.5");
[Only registered and activated users can see links]("Accept-Encoding", "gzip,deflate");
[Only registered and activated users can see links]("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
[Only registered and activated users can see links]("Keep-Alive", "300");
[Only registered and activated users can see links]("Connection", "keep-alive");
if (Referer != "0")
[Only registered and activated users can see links]("Referer", Referer);
if (strCookies != "")
[Only registered and activated users can see links]("Cookie", strCookies);
if (method == "POST") {
[Only registered and activated users can see links](true);
[Only registered and activated users can see links]("Content-Type", "application/x-[Only registered and activated users can see links]");
[Only registered and activated users can see links]("Content-Length", "" + PostData.length());
OutputStreamWriter writePost = new OutputStreamWriter([Only registered and activated users can see links]());
writePost.write(PostData);
writePost.flush();
}
[Only registered and activated users can see links]();
String encoding = [Only registered and activated users can see links]();
InputStream receiving = null;
if (encoding.equalsIgnoreCase("gzip"))
receiving = new GZIPInputStream([Only registered and activated users can see links]());
else if (encoding.equalsIgnoreCase("deflate"))
receiving = new InflaterInputStream([Only registered and activated users can see links](), new Inflater(true));
else
receiving = [Only registered and activated users can see links]();
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 ([Only registered and activated users can see links](i) == null && [Only registered and activated users can see links](i) == null) {
break;
} else if ("Set-Cookie".equalsIgnoreCase([Only registered and activated users can see links](i))) {
String[] fields = [Only registered and activated users can see links](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;
}
}

Ryan~
03-18-2012, 11:56 PM
Anyone know how to use sockopens?

Just something as simple as logging in?

Zachafer
03-19-2012, 12:12 AM
I found that snippet... I can't get it to work for me.


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!


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 = "[Only registered and activated users can see links]";

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+" [Only registered and activated users can see links]");
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-[Only registered and activated users can see links]");
request += ("\r\n");
request += (data+"\r\n");
}
request += ("\r\n");

//updating previously accessed website, i.e. referrer:
referrer = "[Only registered and activated users can see links]"+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:


Connection <variable name> = new Connection ("<domain>");
It is crucial to remember that the "<domain>" starts with "[Only registered and activated users can see links]" and ends with ".de/com/w/e else". NO "[Only registered and activated users can see links]" and NO ending it with "/"
Example:
[Only registered and activated users can see links]
[Only registered and activated users can see links]
After instantiating/declaring an object, there is only one major method to concern yourself with:


<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::POST||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::POST||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:


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. Not sure how reliable it is

Ryan~
03-19-2012, 12:14 AM
I saw that and didn't wanna bother with it.