Results 1 to 5 of 5
  1. #1
    chopficaro is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default HttpURLConnection with POST data

    hi, im asking this question again but now i have well commented and formulated code that should be easy to debug
    im having trouble making an http request with post data
    the guides i have read seem to use a non existent method of the HttpURLConnection class called openOutputStream
    Java Code:
    //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 static void main (String[] args) throws Exception
    	{
    		//enter url and post data in cmd prompt to test
    		String 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
    	{
    		//connection variables
    		String agent = "Mozilla/4.0";
    		String type = "application/x-www-form-urlencoded";
    		HttpURLConnection connection = null;
    		OutputStreamWriter wr = null;
    		BufferedReader rd  = null;
    		StringBuilder sb = null;
    		String line = 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.openOutputStream();
    			os.write( encodedData.getBytes() );
    			//check if theres an http error
    			int rc = connection.getResponseCode();
    			if(rc==200)
    			{
    				//no http response code error
    				//read the result from the server
    				rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    				sb = new StringBuilder();
    				returnString=sb.toString();
    				
    			}
    			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();
    		}
    		return returnString;
    	}
    }
    error
    UrlToString.java:38: cannot find symbol
    symbol : method openOutputStream()

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: HttpURLConnection with POST data

    See the API for HttpURLConnection, there is not method signature openOutputStream. See the getOutputStream method...and be sure to flush/close the OutputStream.

  3. #3
    chopficaro is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: HttpURLConnection with POST data

    ok now it seems my code is sort of working but now i think i am having a tcpip problem
    trying to connect to a test page with the command prompt
    i hope someone knows enough about this to help me
    heres my sort of working code and new error
    Java Code:
    //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 static void main (String[] args) throws Exception
    	{
    		//enter url and post data in cmd prompt to test
    		String 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
    	{
    		//connection variables
    		String agent = "Mozilla/4.0";
    		String type = "application/x-www-form-urlencoded";
    		HttpURLConnection connection = null;
    		OutputStreamWriter wr = null;
    		BufferedReader rd  = null;
    		StringBuilder sb = null;
    		String line = 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
    				ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
    				returnString=(String)in.readObject();
    				in.close();
    				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();
    		}
    	}
    }
    error
    >java UrlToString http://mw3dailymedia.com/twixtor/javagrab.php user=chopficaro&
    pass=omgtkkyb
    search URL connect failed: invalid stream header: 0A6E6F20
    java.io.StreamCorruptedException: invalid stream header: 0A6E6F20
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at UrlToString.<init>(UrlToString.java:47)
    at UrlToString.main(UrlToString.java:12)

    'pass' is not recognized as an internal or external command,
    operable program or batch file.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: HttpURLConnection with POST data

    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    chopficaro is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: HttpURLConnection with POST data

    yay i think its working, this will be a great utility class
    theres still 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 have two output statements
    the one in the constructor works
    the one in main() doesnt. does anyone know why?
    Java Code:
    //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 static void main (String[] args) throws Exception
    	{
    		//enter url and post data in cmd prompt to test
    		String 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();
    		}
    	}
    }

Similar Threads

  1. HttpURLconnection POST request
    By chopficaro in forum Advanced Java
    Replies: 12
    Last Post: 05-21-2012, 02:10 AM
  2. HTTP POST Request (HttpURLConnection)
    By Dude in forum Networking
    Replies: 17
    Last Post: 07-15-2011, 11:11 PM
  3. POST request using HttpUrlConnection
    By turk in forum Networking
    Replies: 3
    Last Post: 07-06-2011, 04:36 PM
  4. POST request using HttpUrlConnection
    By turk in forum Networking
    Replies: 2
    Last Post: 07-06-2011, 04:29 PM
  5. Replies: 4
    Last Post: 10-15-2008, 01:59 PM

Posting Permissions

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