Extreme Makeover Buildism Edition.
#5
I finished the httpGet

Root:
Code:
addFunction("httpGet", new JavaFunction()
        {

            public int call(LuaCallFrame callFrame, int nArguments) {
                if(nArguments != 2 || !(callFrame.get(1) instanceof String))
                        throw new RuntimeException("Argument to httpGet() must be a string");
                    web web=new web();
                    String url = (String) callFrame.get(1);
                   web.set(url);
                    callFrame.push(web.getNoReplace());
                    return 1;
            }
        });

dig.web:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dig;

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author roperson
*/
public class web {
    static URL url;
    
    public static void set(String URL) {
        try {
            // try {
                 //URLEncoder.encode(URL, "UTF-8");
             URL=URL.replaceAll(" ", "+");
             URL=URL.replaceAll(",", "%2C");
             if (URL.startsWith("http://")) {
                 URL.replaceFirst("http://", "");
             }
               URL url2=new URL("http://"+URL);
               url=url2;
        } catch (MalformedURLException ex) {
            throw new RuntimeException("Argument was formatted incorrectly.");
        }
  }
    
      public static String getNoReplace() {
        try {
            String buffer;
            String line;
            int responseCode;
            HttpURLConnection connection;
            InputStream input;
            BufferedReader dataInput;
            connection = (HttpURLConnection) url.openConnection();
            responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                    throw new RuntimeException("HTTP response code: " +String.valueOf(responseCode));
            }
            try {
              buffer = new String();
              input = connection.getInputStream();
              dataInput = new BufferedReader(new InputStreamReader(input));
              while ( (line = dataInput.readLine()) != null) {
                buffer=buffer+line;
              }
              input.close();
            }
            catch (Exception ex) {
              ex.printStackTrace(System.err);
              return null;
            }
            return buffer;
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
            return null;
        }
  }

  public static String get() throws Exception {
    String buffer;
    String line;
    int responseCode;
    HttpURLConnection connection;
    InputStream input;
    BufferedReader dataInput;
    connection = (HttpURLConnection) url.openConnection();
    responseCode = connection.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
      throw new Exception("HTTP response code: " +
                          String.valueOf(responseCode));
    }
    try {
      buffer = new String();
      input = connection.getInputStream();
      dataInput = new BufferedReader(new InputStreamReader(input));
      while ( (line = dataInput.readLine()) != null) {
        buffer=line;
      }
      input.close();
    }
    catch (Exception ex) {
      ex.printStackTrace(System.err);
      return null;
    }
    return buffer;
  }
  
  public static void send() throws Exception {
      get();
  }
  
  public static void send(String url) throws Exception {
      set(url);
      get();
  }
}
[Image: 2eehsib.gif]
Reply


Messages In This Thread
Extreme Makeover Buildism Edition. - by When - 08-18-2012, 01:58 AM

Forum Jump:


Users browsing this thread: 8 Guest(s)