Results 1 to 9 of 9

Thread: Good Java Wrapper?

  1. #1
    Ryan~'s Avatar
    Joined
    Jan 2012
    Posts
    123
    Userbars
    5
    Thanks
    1,380
    Thanked
    1,424/827
    DL/UL
    103/4
    Mentioned
    640 times
    Time Online
    15d 12h 13m
    Avg. Time Online
    5m

    Good Java Wrapper?

    I need a good Java wrapper


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

  2. #2
    Reemer's Avatar
    Joined
    Dec 2011
    Posts
    639
    Userbars
    8
    Thanks
    364
    Thanked
    446/256
    DL/UL
    39/0
    Mentioned
    203 times
    Time Online
    4d 13h 47m
    Avg. Time Online
    1m
    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
    Last edited by Reemer; 03-18-2012 at 10:05 PM.

  3. #3
    Ryan~'s Avatar
    Joined
    Jan 2012
    Posts
    123
    Userbars
    5
    Thanks
    1,380
    Thanked
    1,424/827
    DL/UL
    103/4
    Mentioned
    640 times
    Time Online
    15d 12h 13m
    Avg. Time Online
    5m
    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 HTTPHeaders, it works. but if I visit the link directly on Neo, I get that....how do I get around it like everyone else does?
    Last edited by Ryan~; 03-18-2012 at 10:20 PM.

  4. #4
    Reemer's Avatar
    Joined
    Dec 2011
    Posts
    639
    Userbars
    8
    Thanks
    364
    Thanked
    446/256
    DL/UL
    39/0
    Mentioned
    203 times
    Time Online
    4d 13h 47m
    Avg. Time Online
    1m
    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.

  5. #5
    Ryan~'s Avatar
    Joined
    Jan 2012
    Posts
    123
    Userbars
    5
    Thanks
    1,380
    Thanked
    1,424/827
    DL/UL
    103/4
    Mentioned
    640 times
    Time Online
    15d 12h 13m
    Avg. Time Online
    5m
    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 HttpWrapper and the post looks like:

    strHTML = wrap.post("http://www.neopets.com/neoboards/process_topic.phtml" , "boardType=reply&topic_id="+ boardID + "&board_id="+ boardNumber + "&next=1&message="+ bumpMessage + "&remLen=" + ((Integer)(400 - charCountBM)).toString(), "http://www.neopets.com/neoboards/topic.phtml?topic=" + 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

  6. #6
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    Sockopen's:
    Code:
     
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    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 HTTPWrapper {
            
            public static String strCookies;
            private static String strHTML;
            
            public static void initHTTPWrapper() {
                    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("http://" + URL);
                            HttpURLConnection http = (HttpURLConnection) url.openConnection();
                            http.setFollowRedirects(false);
                            http.setRequestMethod(method);
                            http.setRequestProperty("Host", url.getHost());
                            http.setRequestProperty("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");
                            http.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/
    png,*/*;q=0.5");
                            http.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
                            http.setRequestProperty("Accept-Encoding", "gzip,deflate");
                            http.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                            http.setRequestProperty("Keep-Alive", "300");
                            http.setRequestProperty("Connection", "keep-alive");
                            if (Referer != "0")
                                    http.setRequestProperty("Referer", Referer);
                            if (strCookies != "")
                                    http.setRequestProperty("Cookie", strCookies);
                            if (method == "POST") {
                                    http.setDoOutput(true);
                                    http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                                    http.setRequestProperty("Content-Length", "" + PostData.length());
                                    OutputStreamWriter writePost = new OutputStreamWriter(http.getOutputStream());
                                    writePost.write(PostData);
                                    writePost.flush();
                            }
                            http.connect();
                            String encoding = http.getContentEncoding();
                            InputStream receiving = null;
                            if (encoding.equalsIgnoreCase("gzip"))
                                    receiving = new GZIPInputStream(http.getInputStream());
                            else if (encoding.equalsIgnoreCase("deflate"))
                                    receiving = new InflaterInputStream(http.getInputStream(), new Inflater(true));
                            else
                                    receiving = http.getInputStream();
                            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 (http.getHeaderFieldKey(i) == null && http.getHeaderField(i) == null) {
                                            break;
                                    } else if ("Set-Cookie".equalsIgnoreCase(http.getHeaderFieldKey(i))) {
                                            String[] fields = http.getHeaderField(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;
            }
    }

  7. #7
    Ryan~'s Avatar
    Joined
    Jan 2012
    Posts
    123
    Userbars
    5
    Thanks
    1,380
    Thanked
    1,424/827
    DL/UL
    103/4
    Mentioned
    640 times
    Time Online
    15d 12h 13m
    Avg. Time Online
    5m
    Anyone know how to use sockopens?

    Just something as simple as logging in?

  8. #8
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    I found that snippet... I can't get it to work for me.

    Quote Originally Posted by slimemonkey
    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.
    Not sure how reliable it is

  9. #9
    Ryan~'s Avatar
    Joined
    Jan 2012
    Posts
    123
    Userbars
    5
    Thanks
    1,380
    Thanked
    1,424/827
    DL/UL
    103/4
    Mentioned
    640 times
    Time Online
    15d 12h 13m
    Avg. Time Online
    5m
    I saw that and didn't wanna bother with it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •