in this class i have a constructor and a main() function for testing the constructor
theres a couple things stopping me from testing it in the command prompt tho
the post data is a string with an ampersand in it and when i try to pass it to the main function in the command prompt it thinks that the ampersand means something, i dont know how to "escape" it
also, i would like to know how to pass a value from the constructor to main() so i can print string test, i tried putting it outside both meathods into the class but it gave me non static context error
//takes am address, and post data, and returns the html as a string import java.net.*; import java.io.*; import java.lang.*; public class UrlToString { public String test = ""; public static void main (String[] args) throws Exception { //enter url and post data in cmd prompt to test new UrlToString (test,args[0],args[1]); System.out.println(test); } //paramaters of returned string, address to get it from, and post data in th for of variable1=value1&variable2=value2... UrlToString (String returnString, String inAddr, String rawData) throws Exception { returnString = ""; //connection variables String agent = "Mozilla/4.0"; String type = "application/x-www-form-urlencoded"; HttpURLConnection connection = null; String encodedData = URLEncoder.encode( rawData, "UTF-8"); //connect try { URL searchUrl = new URL(inAddr); connection = (HttpURLConnection)searchUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty( "User-Agent", agent ); connection.setRequestProperty( "Content-Type", type ); connection.setRequestProperty( "Content-Length", Integer.toString(encodedData.length()) ); OutputStream os = connection.getOutputStream(); os.write( encodedData.getBytes() ); os.flush(); os.close(); //check if theres an http error int rc = connection.getResponseCode(); if(rc==200) { //no http response code error //read the result from the server InputStreamReader in = new InputStreamReader(connection.getInputStream()); BufferedReader br = new BufferedReader(in); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { returnString = returnString.concat(strLine); System.out.println(strLine); } return; } else { System.out.println("http response code error: "+rc+"\n"); return; } } catch( IOException e ) { System.out.println("search URL connect failed: " + e.getMessage()); e.printStackTrace(); } } }