Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: HttpURLConnection with POST data URLEncoder.encode not working i think

  1. #1
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HttpURLConnection with POST data URLEncoder.encode not working i think

    hi all
    i want to make an HttpURLConnection with POST data
    this code works for the most part, except for the post data part
    i tested it on this page, with this post data, with this command line in windows command prompt:

    >java UrlToString http://mw3dailymedia.com/twixtor/javagrab.php user=chopficaro^
    &pass=omgtkkyb

    the ^ is required to escape the & in windows command prompt
    and it returned to me

    no username or password

    i am absolutely sure that the php is correct, i can access this page with the user name and password specified with an html form
    if it were the wrong username or password it would return "wrong user name or password"
    so it did not send the post data.
    why?
    //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 returnString = "";
    	public static void main (String[] args) throws Exception
    	{
    		//enter url and post data in cmd prompt to test
    		UrlToString urlToString = new UrlToString  (args[0],args[1]);
    		System.out.println(urlToString.returnString);
    	}
    	//paramaters of returned string, address to get it from, and post data in th for of variable1=value1&variable2=value2...
    	UrlToString (String inAddr, String rawData) throws Exception
    	{
    		//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);
    				}
    				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();
    		}
    	}
    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: HttpURLConnection with POST data URLEncoder.encode not working i think

    Print out the value of encodedData.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: HttpURLConnection with POST data URLEncoder.encode not working i think

    Are you sure your PHP is right? This also doesn't work:

    curl -s -v  -d username=chopficaro -d password=omgtkkyb --trace - http://mw3dailymedia.com/twixtor/javagrab.php

  4. #4
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HttpURLConnection with POST data URLEncoder.encode not working i think

    the server doesnt have curl installed
    yes the php is right because i can access the data with an html form
    encoded data prints as:
    user%3Dchopficaro%26pass%3Domgtkkyb
    Last edited by chopficaro; June 6th, 2012 at 01:22 PM.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: HttpURLConnection with POST data URLEncoder.encode not working i think

    D'oh! That command works just fine if I get the names of the form data right:
    sean@bulldozer:~$ curl -d user=chopficaro -d pass=omgtkkyb --trace - http://mw3dailymedia.com/twixtor/javagrab.php== Info: About to connect() to mw3dailymedia.com port 80 (#0)
    == Info:   Trying 69.61.35.28... == Info: connected
    => Send header, 259 bytes (0x103)
    0000: 50 4f 53 54 20 2f 74 77 69 78 74 6f 72 2f 6a 61 POST /twixtor/ja
    0010: 76 61 67 72 61 62 2e 70 68 70 20 48 54 54 50 2f vagrab.php HTTP/
    0020: 31 2e 31 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 1.1..User-Agent:
    0030: 20 63 75 72 6c 2f 37 2e 32 32 2e 30 20 28 78 38  curl/7.22.0 (x8
    0040: 36 5f 36 34 2d 70 63 2d 6c 69 6e 75 78 2d 67 6e 6_64-pc-linux-gn
    0050: 75 29 20 6c 69 62 63 75 72 6c 2f 37 2e 32 32 2e u) libcurl/7.22.
    0060: 30 20 4f 70 65 6e 53 53 4c 2f 31 2e 30 2e 31 20 0 OpenSSL/1.0.1 
    0070: 7a 6c 69 62 2f 31 2e 32 2e 33 2e 34 20 6c 69 62 zlib/1.2.3.4 lib
    0080: 69 64 6e 2f 31 2e 32 33 20 6c 69 62 72 74 6d 70 idn/1.23 librtmp
    0090: 2f 32 2e 33 0d 0a 48 6f 73 74 3a 20 6d 77 33 64 /2.3..Host: mw3d
    00a0: 61 69 6c 79 6d 65 64 69 61 2e 63 6f 6d 0d 0a 41 ailymedia.com..A
    00b0: 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 43 6f 6e 74 ccept: */*..Cont
    00c0: 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 32 39 0d 0a ent-Length: 29..
    00d0: 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 Content-Type: ap
    00e0: 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 77 77 2d plication/x-www-
    00f0: 66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 0d form-urlencoded.
    0100: 0a 0d 0a                                        ...
    => Send data, 29 bytes (0x1d)
    0000: 75 73 65 72 3d 63 68 6f 70 66 69 63 61 72 6f 26 user=chopficaro&
    0010: 70 61 73 73 3d 6f 6d 67 74 6b 6b 79 62          pass=omgtkkyb
    == Info: upload completely sent off: 29out of 29 bytes
    <= Recv header, 17 bytes (0x11)
    0000: 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP/1.1 200 OK.
    0010: 0a                                              .
    <= Recv header, 37 bytes (0x25)
    0000: 44 61 74 65 3a 20 57 65 64 2c 20 30 36 20 4a 75 Date: Wed, 06 Ju
    0010: 6e 20 32 30 31 32 20 31 38 3a 32 35 3a 33 38 20 n 2012 18:25:38 
    0020: 47 4d 54 0d 0a                                  GMT..
    <= Recv header, 16 bytes (0x10)
    0000: 53 65 72 76 65 72 3a 20 41 70 61 63 68 65 0d 0a Server: Apache..
    <= Recv header, 26 bytes (0x1a)
    0000: 58 2d 50 6f 77 65 72 65 64 2d 42 79 3a 20 50 48 X-Powered-By: PH
    0010: 50 2f 35 2e 32 2e 31 37 0d 0a                   P/5.2.17..
    <= Recv header, 19 bytes (0x13)
    0000: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 Connection: clos
    0010: 65 0d 0a                                        e..
    <= Recv header, 28 bytes (0x1c)
    0000: 54 72 61 6e 73 66 65 72 2d 45 6e 63 6f 64 69 6e Transfer-Encodin
    0010: 67 3a 20 63 68 75 6e 6b 65 64 0d 0a             g: chunked..
    <= Recv header, 25 bytes (0x19)
    0000: 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 65 Content-Type: te
    0010: 78 74 2f 68 74 6d 6c 0d 0a                      xt/html..
    <= Recv header, 2 bytes (0x2)
    0000: 0d 0a                                           ..
    <= Recv data, 6 bytes (0x6)
    0000: 31 0d 0a 0a 0d 0a                               1.....
     
    <= Recv data, 24 bytes (0x18)
    0000: 31 32 0d 0a 63 6f 64 65 30 20 45 34 31 62 30 53 12..code0 E41b0S
    0010: 37 79 5a 58 6f 0a 0d 0a                         7yZXo...
    code0 E41b0S7yZXo
    <= Recv data, 96 bytes (0x60)
    0000: 35 61 0d 0a 63 6f 64 65 31 20 62 35 45 6f 38 36 5a..code1 b5Eo86
    0010: 43 66 37 6c 38 0a 63 6f 64 65 32 20 55 33 4b 4a Cf7l8.code2 U3KJ
    0020: 6b 4f 6d 78 54 41 41 0a 63 6f 64 65 33 20 73 2d kOmxTAA.code3 s-
    0030: 71 71 71 66 51 48 71 55 6f 0a 63 6f 64 65 34 20 qqqfQHqUo.code4 
    0040: 4c 6b 74 59 57 39 6b 57 5a 64 6f 0a 63 6f 64 65 LktYW9kWZdo.code
    0050: 35 20 50 4a 43 6a 78 69 71 38 6e 73 30 0a 0d 0a 5 PJCjxiq8ns0...
    code1 b5Eo86Cf7l8
    code2 U3KJkOmxTAA
    code3 s-qqqfQHqUo
    code4 LktYW9kWZdo
    code5 PJCjxiq8ns0
    <= Recv data, 42 bytes (0x2a)
    0000: 32 34 0d 0a 63 6f 64 65 36 20 7a 45 48 36 4d 4d 24..code6 zEH6MM
    0010: 4a 75 6b 72 63 0a 63 6f 64 65 37 20 42 5f 35 56 Jukrc.code7 B_5V
    0020: 76 56 37 7a 32 38 63 0a 0d 0a                   vV7z28c...
    code6 zEH6MMJukrc
    code7 B_5VvV7z28c
    <= Recv data, 91 bytes (0x5b)
    0000: 33 37 0d 0a 63 6f 64 65 38 20 4b 4a 61 7a 41 71 37..code8 KJazAq
    0010: 50 50 7a 73 6b 0a 63 6f 64 65 39 20 47 4b 57 65 PPzsk.code9 GKWe
    0020: 43 6c 47 79 67 5f 45 0a 63 6f 64 65 31 30 20 75 ClGyg_E.code10 u
    0030: 59 70 51 6a 39 57 33 30 41 67 0a 0d 0a 31 33 0d YpQj9W30Ag...13.
    0040: 0a 63 6f 64 65 31 31 20 61 49 58 71 47 6f 70 5a .code11 aIXqGopZ
    0050: 6f 54 6b 0a 0d 0a 30 0d 0a 0d 0a                oTk...0....
    code8 KJazAqPPzsk
    code9 GKWeClGyg_E
    code10 uYpQj9W30Ag
    code11 aIXqGopZoTk
    == Info: Closing connection #0

    URLEncoder is for encoding URLs. You're encoding form data for an application/x-www-form-urlencoded POST. That's different! Compare the content of the POST I've copied above from curl with the String you've just posted.

  6. #6
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HttpURLConnection with POST data URLEncoder.encode not working i think

    ok well i tried not encoding it with os.write( rawData );
    but it says no such method exists os.write( String );

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: HttpURLConnection with POST data URLEncoder.encode not working i think

    You used OutputStream.write() with a String in your original code.

  8. #8
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HttpURLConnection with POST data URLEncoder.encode not working i think

    OMG ITS WORKING THANK U SO MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Last edited by chopficaro; June 6th, 2012 at 03:18 PM.

Similar Threads

  1. BufferedReader hangs when handling POST method data
    By Sucker Punch in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 04:33 PM
  2. encode a png image in ascii format..
    By entwicklerin in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: January 16th, 2012, 10:17 AM
  3. Passing session ID over HttpURLConnection
    By zuzacat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 20th, 2011, 09:18 AM
  4. Can't form a simple POST or GET http request. No data returns.
    By goodguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2011, 05:04 AM
  5. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM